fisher123.com JavaScript Loops

For Loop

Let's count to 10!

That was too easy!

How this for loop works:

There are three parts to the for loop:

for (i = 1; i<=5; i=i+1)

  1. i=1 this sets the starting value for the variable
  2. i <= 5 this condition controls the number of times the loop will repeat
  3. i = i +1 this defines the incremental value. Every time the loop runs the program will add 1 to i

TRY IT!

You will use the loop to create a delay.

  1. Write an HTML document with a for loop that counts to 10,000,000 (yes ten million)
  2. When the script is done counting an alert will pop up that says "all done"
  3. HINT you don't want the numbers to show up on the page. The script is just used to count to 10,000,000

check it out here