bbb Home |
Lesson 10 - Keep Track of Score and Number of Targets |
SUMMARY
In this lesson you will learn how to use variables in order to store and manipulate data. You will also learn about the innerHTML property of div tags. DISCUSSION
Variables are used to hold and manipulate (add, subtract,...) data. Variables
can store different types of data including numbers and strings. Variables
have different scope depending on where they are declared. For example, if
a variable is declared in a function, it can only be used in the function. If a
variable is declared outside of a function then it can be used by any function in
the program. The details of variables are listed below.
Variables
Variable Types
Variable Scope - Global vs. Local
innerHTML You can refer to the contents of a <div> by using its innerHTML property. For example, as shown in the example, the letter "a" can be displayed in the div using the line: divA.innerHTML = "<b>a</b>" This example also illustrates how HTML tags are rendered when used in the innerHTML property of a <div>. When you run the example, you will see that the <b> tags are not displayed; instead, the letter "a" appears as bold. EXAMPLE - Variables and innerHTMLOn line 4, a variable called a is declared and initialized to 0. Initializing means that the variable is assigned a value. If you do not initialize a variable, then it is undefined. Because this variable is declared outside of all functions, it is global. Global variables remain in memory throughout the life of the program and can be accessed by all functions. Generally you should avoid using global variables unless you absolutely must. On line 6, a variable called b is declared and initialized to 0. Because it is declared in a function, it can only be used in the function. Also, because it is local to the function, it is cleared from memory after the function executes. On line 7, variable c is declared. It is not initialized. On line 8, the values of the variables are displayed in an alert. Notice that when we use the variables we do NOT put quotations around them. If you put quotations around the variables, then the variable names would be displayed since the quotations make them strings. On line 9, we add 1 to the value of a. In programming, the variable on the left side of the = sign is assigned the value from the right side. On line 9, 1 is added to the value of a. We could have used a++ instead of a=a+1to add 1 to a. On line 10, we add 1 to b. On line 11 we set c equal to the value of 0.5 - a. On line 12 we set the contents of divA to "a = [the value of a]". Notice that we can embed HTML tags in the innerHTML of a <div> so that the a is in bold. We can also combine strings using the + sign. Lines 13 and 14 set the innerHTML of divB and divC. Run the program and click on the page several times. Observe the values of a, b, and c in the alert and in the <div>'s. Notice that the value of a keeps increasing by 1. This is because a is global and so it remains in memory throughout the execution of the program. Notice that the value of b remains constant in the alert and <div> no matter how many times you click on the page. The variable b is local so it is created every time the function is called. After the function is done executing, the variable is released from memory. Generally we should make variables local unless we have a specific reason to make them global. As we write more complicated programs, the drawbacks of global variables will become more apparent. As you click on the page, notice the value of c. When displayed in the alert, c is undefined since it was never assigned a value. When displayed in divC, it has a value because it was assigned the value of 0.5 - a on line 11.
PRACTICE
GAME DEVELOPMENTModify your game page (game.htm) so that the score is displayed in the <div>. The <div> should display the number of times that the target is moved and the number of hits. For example, if you hit the target 5 out of 7 times, then the <div> should display 5/7. Modify your page as follows:
Test your page. As the target moves, the number of tries should increase. If you click on the target, the number of hits should increase. In the next lesson we will add code to end the game.
|
References |
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com |