...

View Full Version : replacing multiple alert boxes with 1 loop



adoreae
07-07-2003, 04:31 PM
I need to find out how to replace the five alert boxes with one "for" loop. Here is the code that I have written. Any help would be greatly appreciated.



<strong>Greetings to Friends!</strong> <br />
<SCRIPT LANGUAGE="JavaScript">
/* The following instructions create an array named greetings with five elements and store various greetings in elements 0 to 4 of the array*/
var greetings = new Array (5)
greetings[0] ="Hi"
greetings[1] ="Hello"
greetings[2] ="How are you,"
greetings[3] ="Good to see you,"
greetings[4] ="Have a good day,"
/*The following instructions create an array named names with five elements and store various names in elements 0 to 4 of the array*/

var names = new Array(5)
names [0] = "John"
names [1] = "Jack"
names [2] = "Jenny"
names [3] = "Jill"
names [4] = "Jake"

/*The following instructions write the first greeting and then the first name in an alert box, then the second greeting and the second name in the next alert box, and so on*/
alert(greetings[0] + names[0])
alert(greetings[1] + names[1])
alert(greetings[2] + names[2])
alert(greetings[3] + names[3])
alert(greetings[4] + names[4])
</script>

ellisd5
07-07-2003, 05:24 PM
This is how you do it with a for loop



for (i=0; i<names.length; i++) {
alert(greetings[i] + names[i])
}


i=0 initises the variable i (spelling rubbish i know)
the i++ increments its value by one which loop.
It does this until i<names.length
names.length is how many itmes are in your array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum