Departments
 Info
   Home Page
   Articles
   Software Goodies

 Explanations
   How do they do that?
   The Web in Focus

 Resources
   Web Site Stuff
   Tech Books
   E-Books
   Tech Toys
   Web Hosting
   Links

 Women
   Opinion
   Tech Women
   Women's Studies

 Tech News
   Computer Security
   Databases
   Java
   Linux
   MP3
   PC Software
   Robotics
   Site Owner
   Tech Latest
   Web Development
   XML

 Spread the Word
   Newsletter
   Recommend this Page

 Site Info
   Legal Disclaimer
   Privacy
   Contact

 Lighter Side
   Crazy for Life
   crazy for romance
   Crazy for Kitties
   Crazy for Dogs
   Crazy for Cows

Copyright 2000-2001, hertechnology.com

 
The Web in Focus
Once More, with Feeling!

JavaScript loop syntax

Unfortunately, we can't just say "repeat this 6 times" in a program like it's written above. Instead, we need to use the specific format, in this case, what JavaScript is expecting. There are different types of loops; we'll be using the very handy "for loop".

for loops have a particular syntax that follows this pattern:

for (i=1; i<=6; i++) {
    do something here
}

What's all that stuff mean?

  1. The term "for" signals the start of a for loop.
  2. A for loop uses a counter to keep track of how many times to do something. For the example of picking 6 lottery numbers, we want to do something six times. If you count 1, 2, 3, 4, 5, 6, you've got the six times.
  3. So, we pick a counter, here we called it "i". Why "i"? "i" is a traditional name for a counter (also called a loop variable) that got started long ago. You could call it something else.
  4. We start the counter off with a particular value, here one: i=1;
  5. We end the counter with a value, here six: i<=6; Technically, this is a termination test for whether to continue looping. We're actually saying to keep looping as long as our counter is a number less than or equal to 6.
  6. We tell the counter to increment itself by 1 each time: i++
  7. The use of the parenthesis is a requirement of JavaScript, so we put them around our counter setup, termination test, and increment.
  8. The semicolons separate the setup and the termination from each other and from the increment.
  9. The open curly brace { is used to tell JavaScript that the stuff that follows is what should be repeated.
  10. The close curly brace } tells JavaScript that it's reached the end of the stuff to repeat.

Some other examples:

Loop 10 times: for (i=1; i<=10; i++)
Loop 10 times in reverse (counting down): for (i=10; i>=1; i--)
Loop 10 times skipping every other number (count by 2's): for (i=1; i<=20; i+=2)

We will fill in what to do to pick our lottery numbers next. We're going to use some JavaScript math routines to do this. Math.random chooses a random floating point number (a number with a decimal place in it like 25.87). But lottery tickets are numbers without decimals, so we'll use Math.floor to give us just the number with the decimal part cut off (25). We'll pick numbers from 1 to 49, like so:

number = (Math.floor(Math.random()*48)) + 1;

What this math-y looking stuff does is pick a random floating point number between 0 and 48, remove the fractional part and add 1 so we end up with a number from 1 to 49.

Finally, we want to print it to the screen, so we'll use document.writeln(number + "<br>");

The fourth program

<html>
<head><title>Fourth Program</title>
</head>
<body>
Your lottery numbers:
<SCRIPT>
<!--
for (i=1; i<=6; i++) {
    number=(Math.floor(Math.random()*48))+1;
    document.writeln(number + "<br>");
}
//-->
</SCRIPT>
</body>
</html>

The program as it stands has one flaw -- nothing prevents it from picking the same number more than once. We can fix that, but it requires some programming beyond the basics we're covering here. But in any case, give it a try. If you reload or refresh the page, you will get a different list of numbers.

This might be more than you wanted to know about programming. But if nothing else, you can take away the concept that programming languages (most of them) have the notion of looping -- doing basically the same thing multiple times.

Tech Books

Tech Toys

More News

  Want to bookmark this site? Press Control-D. (Hold down the control key and the letter D at the same time.)