HTML and CSS Reference
In-Depth Information
Incrementing the Position Locator Variable After declaring the scrollingMsg()
function, the next step is to increment the beginPos variable and append the next
character from the message string to the text field. To cause the message to scroll in the
text field, the position locator variable (beginPos) must be incremented by one. Table 10-3
describes the various ways JavaScript statements can be used to increment variables.
Table 10-3 Incrementing a Variable
Statement
Explanation
variable=variable+1
Executes the expression on the right side of the equal sign and assigns the result
to a variable on the left side
variable+=1
Adds the number after the equal sign to a variable
variable++
Adds 1 to a variable; increments after the assignment
++variable
Adds 1 to a variable before the assignment
Once incremented, the new value of the position locator variable, beginPos, allows
the substring() method in line 11 to extract the next character in the message string and
append it to the end of the message in the text field.
To Enter the Code to Increment the Position Locator Variable
The following step enters the code to increment the position counter.
1
If necessary, click line 12
and indent under the
previous line.
beginPos=
beginPos+1 to
increment the position
locator by one and
then press the e n t e r
key (Figure 10-6).
Why did we write the
increment statement
this way instead of
using one of the other
methods?
This method is the most common and easiest for beginners to understand. In addition, developers should use a format
that can be recognized by anyone who might have to modify their code after the initial implementation.
Type
increments position by one
to determine next character
to be added to message
Figure 10-6
Entering an if Statement After incrementing the position location variable
(beginPos) by one, the JavaScript code must determine if the current value of beginPos
exceeds the length of the message string. An if statement is used to test a condition
and then take one or more actions, based on the results of the test. The general form
of the if statement is shown in Table 10-4 on the next page. The if statement tests
a condition , which is any comparison of values that evaluates to true or false. If the
result of the comparison is true, the JavaScript code within the braces is executed.
If the result of the comparison is false, the code after the closing brace is executed.
Operands in a
Conditional Evaluation
An operand is a numerical,
string, logical, or object
data type or value.
Operands must be of
the same data type, or
you will not get a true
comparison result.
 
Search WWH ::




Custom Search