Tutorial
Click on thumbnailed images to enlarge
There are various looping method in Javascript but here I'm just going to talk about the
for() loop.
Let us first see how it works.
The above codes will output:
[quote]monkey LOVES banana
tiger LOVES human
elephant LOVES nuts[/quote]
Okay, so it picks up from the arrays and writes everything automatically. But, how useful is it?
Try replacing the array values to write a navigation menu using table or div tags.
If you have a list of links, try listing 'em using the for() loop. Saves time.
for() loop.
Let us first see how it works.
<script type="text/javascript">
d = document; // the document object is replaced with d to save time typing :P
animals = new Array("monkey", "tiger", "elephant"); // first array: animals!
food = new Array("banana", "human", "nuts"); // second array: food :) yummy...
for(i=0; i<animals.length && i<food.length; i++){
d.write(animals[i]+" LOVES "+food[i]+"<br />");
}
</script>
d = document; // the document object is replaced with d to save time typing :P
animals = new Array("monkey", "tiger", "elephant"); // first array: animals!
food = new Array("banana", "human", "nuts"); // second array: food :) yummy...
for(i=0; i<animals.length && i<food.length; i++){
d.write(animals[i]+" LOVES "+food[i]+"<br />");
}
</script>
The above codes will output:
[quote]monkey LOVES banana
tiger LOVES human
elephant LOVES nuts[/quote]
Okay, so it picks up from the arrays and writes everything automatically. But, how useful is it?
Try replacing the array values to write a navigation menu using table or div tags.
If you have a list of links, try listing 'em using the for() loop. Saves time.
Tutorial Comments
Showing latest 3 of 3 comments

Well, the purpose of this tutorial is to give the basic example on how the loop works. So, the expected result is that people can use this concept to loop on something else. The main idea is, to help people not to retype everything over and over again. E.
By birdman on May 1, 2006 6:18 am
QUOTE(chasingvictory @ Apr 24 2006, 3:34 PM) [snapback]2011775[/snapback]hey that like java!!1 haha.. i did not know that...JavaScript, actually.I feel like there should be more info in this tutorial, specifying what exactly the stuff is
By mipadi on Apr 24, 2006 4:26 pm
hey that like java!!1 haha.. i did not know that...
By chasingvictory on Apr 24, 2006 3:34 pm