HTML and CSS Reference
In-Depth Information
Table 10-25 (continued)
Line
Code
91
if (formatDollars.substring(0,1) == “,”)
92
dollars = formatDollars.substring(1,formatDollars.length)
93
if…else
94
dollars = formatDollars
95
}
Because the dollar portion of the value is still a string, line 79 uses the length
property to determine how many digits are in the dollar portion. The if statement at
line 80 determines if the dollar portion of the value is longer than three digits. If that is
true, then a while loop routine (lines 81 through 90) places a comma every three digits.
Line 82 extracts three digits starting from the right by subtracting 3 from the length of
the dollar value (dollarLen). The if statement at Line 83 verifies three digits and line 84
inserts a comma in the output string. Line 85 decrements the length of the dollar value
to look for the next group of three digits. When no more groups of three digits exist, the
length of dollarLen is set to zero (line 88) and the loop terminates at line 90. The state-
ments in lines 91 through 95 prevent the code from inserting a comma if only three digits
are to the left of the decimal point.
To Enter an if...else Statement and while Loop to Extract the Dollar Portion
of the Output and Insert Commas
The following step enters the if…else statement and while loop to extract the dollar portion of the output and
insert commas into the output if needed.
1
line 79.
If necessary, click
determines number of digits in
dollar amount for placement
of commas every 3 digits
Enter the JavaScript
code as shown in
Table 10-25 starting
on the previous page,
indenting as shown
to extract the dollar
portion of the output,
insert the appropriate
commas, and then
press the e n t e r key
(Figure 10-30).
determines if commas
should be placed
every three digits, and
places them
strips off any
extra commas
not needed
Figure 10-30
Reconstructing the Formatted Output and Returning the Formatted
Value Next, the JavaScript statements must be written to reconstruct (concatenate)
the formatted dollars and cents output into a formatted payment amount value, store the
payment amount value in the formatValue variable, and return the formatValue variable.
Table 10-26 shows the statements needed to complete this task.
 
Search WWH ::




Custom Search