JavaScript:Iteration

Commonly used iteration functions
Javascript iteration functions are very similar to those used in C++

The three commonly used iteration functions are: 

while ... (js3_01)


for ... (js3_02)


do ...while (js3_03)


Using an inline function
In the previous example the JavaScript was embedded in the code. How can you make the html/JavaScript easier to read? We can do this by separating the JavaScript from the html code by using a function. (This could be improved further by having the JavaScript stored in a linked .js file)



Example :.

Exercise 1: Check box using iteration (js2_05 modified)
Modify the following program to use an iteration technique in fnCheck. Note the values have been changed to the cost of each item. Assume this is a daily order and only get none or one of each (e.g. you probably would by cream and sugar daily).

Example :.