HTML and CSS Reference
In-Depth Information
To Enter the dollarFormat() Function
The following step enters the dollarFormat() function and initialize the variables.
1
the e n t e r key once.
Click line 73 and press
split() used to find decimal point in value
by searching for period. 2 indicates into
how many items string should be split.
Items stored in an array.
the JavaScript code
from Table 10-22
on the previous
page to begin the
dollarFormat()
function and then
press the e n t e r key
(Figure 10-28).
On line 74, enter
dollar amount is
stored in element
zero [0]
Figure 10-28
Plan
Ahead
Using a while loop to insert commas every three digits in a number.
To place a comma every three digits, use a while loop. The following steps describe the logic
of the while loop:
1. Extract three digits from the dollar value, starting from the right by subtracting 3 from
the length of the dollar value (dollarLen)
2. Verify that three digits have been subtracted and then insert a comma in the output string
3. Decrement the length of the dollar value to look for the next group of three digits
4. The process (loop) is complete when no more groups of three digits exist and the length
of dollarLen is zero
Using a while Loop and if...else Statement to Extract the Dollars Portion
and Insert Commas A loop is a series of statements that executes repeatedly until
it satisfies a condition. JavaScript has two types of loops: for loops and while loops. Both
types of loops use the logic illustrated by the flowchart in Figure 10-29. Both loops first
test a condition to determine if the instructions in the loop are to be executed.
while loop
Figure 10-29
 
Search WWH ::




Custom Search