Lesson Eight: Parameters
- Your current JavaScript file should look like this:
var event1;
event1 = "488 Million Years Ago the Mollusks Appeared";
var event2 = "250 Million Years Ago the Dinosaurs Appeared";
var event3 = "65 Million Years Ago - Age of the Mammals";
var event4 = "60 Million Years Ago - The Primates";
var event5 = "5.2 Million Years Ago - The Hominids";
var eventchoice = 4;
function historyevents() {
if (eventchoice == 1) { document.write(event1); }
else if (eventchoice == 2) { document.write(event2); }
else if (eventchoice == 3) { document.write(event3); }
else if (eventchoice == 4) { document.write(event4); }
else { document.write(event5); }
}
historyevents();
- You are going to set the value of the eventchoice variable with a function parameter; so remove the = 4 code from the variable statement on line seven. Line seven should now read: var eventchoice;.
- Parameters are values that are passed into a function. Parameters are placed inside the () parenthesis that are included with the function statement. Parameters are also variables. Add the eventnumber parameter as shown below:
function historyevents(eventnumber) {
- Set the value of the eventchoice variable by adding this code after the above line:
function historyevents(eventnumber) {
eventchoice = eventnumber;
- Now you can pass the value of the eventchoice variable in the paranthesis of the function calling code:
historyevents(4);
Test your program with the new code. Your page should look like this:
- Experiment by putting different values in the calling code parameter and executing your program.
Lesson Nine: The Button Object