HTML and CSS Reference
In-Depth Information
The for loop relies on a conditional statement using numeric values and thus
is often referred to as a counter-controlled loop. Table 10-23 shows the general form
of the for loop.
Table 10-23 for Loop
General form: for (start; stop; counter-control) {
JavaScript statements
}
Comment:
where start is a variable initialized to a beginning value; stop is an expression indicating the
condition at which the loop should terminate; and the counter-control is an expression indicating
how to increment or decrement the counter. Semicolons separate the three variables.
Examples:
for (j=1; j<5; j++) {
for (ctr=6; ctr>0; ctr--) {
for (itemx=1; itemx<10; itemx=itemx+2) {
The while loop relies on a conditional statement that either can use a numeric
value or a string. Unlike the for loop, which will execute for a specified number of times,
the while loop will execute until the specified condition is no longer true. Table 10-24
shows the general form of the while loop.
Table 10-24 while Loop
General form: while (condition) {
JavaScript statements
}
Comment:
where condition is either a numeric value or a string; and the JavaScript statements execute while
the result of the condition is true.
Examples:
while (ctr < 6) {
while (isNaN(temp)) {
while (Response != “Done”) {
In this chapter, the while loop is used in formatting the dollar value of the Monthly
Payment value. The dollars portion is represented by the digits to the left of the decimal
point. If the dollars portion of the mortgage payment contains more than three digits,
commas need to be inserted. Table 10-25 shows the JavaScript statements used to
determine the length of the dollar value and placement of the commas.
Table 10-25 Code for Determining the Length of the Dollar Value and Comma Placement
Line
Code
var dollarLen = dollars.length
79
if (dollarLen > 3) {
80
while (dollarLen > 0) {
81
tempDollars = dollars.substring(dollarLen - 3,dollarLen)
82
if (tempDollars.length == 3) {
83
formatDollars = “,”+tempDollars+formatDollars
84
dollarLen = dollarLen - 3
85
} else {
86
formatDollars = tempDollars+formatDollars
87
dollarLen = 0
88
}
89
}
90
Search WWH ::




Custom Search