In this section I will show you how to create and use Loops in your
JavaScripts.
Loops allow the user to automatically repeat an action.
In the example below I'm going to create an
array of
football players from the NFL Washington Redskins. My script will List
10 players using a for loop from an array that I'll call redSkins.
<script type="text/javascript">
<!-- This is the definition of the array
var redSkins = new Array();
redSkins[0] = "#26 Clinton Portis";
redSkins[1] = "#8 Mark Brunell";
redSkins[2] = "#45 Mike Sellers";
redSkins[3] = "#89 Santana Moss";
redSkins[4] = "#82 Antwan Randel-El";
redSkins[5] = "#17 Jason Campbell";
redSkins[6] = "#21 Sean Taylor";
redSkins[7] = "#93 Marcus Washington";
redSkins[8] = "#47 Chris Cooley";
redSkins[9] = "#46 Ladell Betts";
//Here is the for loop it starts with the first object
in the array redSkins[0] and increases by //one until redSkins[9] is
reached then the loop ends..
for ( var skins = 0; skins < 10; skins++) {
document.write(redSkins[skins]);
document.write( "<br>" );
}
//-->
</script>
The above script will produce the following list of
Redskin players:
|