JavaScript Primers #24

By Joe Burns

http://www.htmlgoodies.com/primers/jsp/article.php/3478371/JavaScript-Primers-24.htm (Back to article)

Use these to jump around or read it all


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment


The Concept

All programming languages have a branching method. The branching method in JavaScript is IF, which we just looked at. All programming languages also have looping techniques. JavaScript has two: While loops and For loops.

In general, you use For loops when you know how many times you want to perform a loop. Use While loops when you are not sure how many times you want to perform a loop. In this example we will look at a For loop.


The Script

 
<HTML>
<HEAD>
</HEAD>
<BODY>
<H3>We'll now count from one to five:</H3>
<SCRIPT type="text/javascript">
   for (i=1; i<=5; i=i+1)
   {
      document.write(i + "<BR>");
   }
 </SCRIPT>
<
...and we're done
</BODY>
</HTML>

The Script's Effect

We'll now count from one to five

...and we're done.


Deconstructing the Script


What You Have Learned


Your Assignment

Write an HTML document that has "Hold on to Your Hat!" surrounded by <H2> tags right at the top of the page. Start with a white background. Then use JavaScript to count to 10000. (Yes, that is 10000. Do not use commas in For statements!)

At that point the background color should change to yellow and text should pop up that reads, "Here comes another color surprise...".

Count to ten grand again. The background color should then change to pink. Good luck.

HINT: You do not have to put any statements between {brackets}.

Here's a possible answer to this assignment
(this will open a new window)


The Concept
The Script
The Script's Effect
Deconstructing the Script
What You've Learned
Your Assignment

On To JavaScript Primer #25