Continued from page 1
for i=0 to 5 document.write names(i) & " " next
There is a nested loop here. Study flow of for i=0 to 4 loop. The outer loop holds index of item that will be compared with every other, next item. For instance, when first cycle of loop begins, i is zero, and when program ends nested loop, j starts at i+1, that is, 1. So turn by turn, item at index zero is compared with every item in array. The outer array goes only till 4 because when nested loop begins, second last item will be compared with last item: j=i+1
Save file, and check it out in your browser.
Now we see use of a dynamically defined array, and how that array can be filled using inputbox() function.
strings is an array that is re-dimensioned whenever user types in a new string. When we declare an array, we reserve memory space for that array, that is, if we say
it means we are reserving space for twenty data items. But what if user enters just five items? The rest of space is wasted. To avoid this, we declare a dynamic array.
In above script, we have a loop whose continuance depends on user input. It keeps on loop until user enters "fin". So, when "fin" is not entered, only then dimension of array strings is increased.
When we are re-dimensioning an array, we have to use keyword preserve to preserve existing content of array. In its absence, array gets re-dimensioned, but values are lost.
Exit do forces program to come out of loop if condition that terminates it, occurs.
Amrit Hallan is a freelance web designer. For all web site development and web promotion needs, you can get in touch with him at firstname.lastname@example.org or http://www.bytesworth.com. For more such articles, visit http://www.bytesworth.com/articles You can subscribe to his newsletter [BYTESWORTH REACHOUT] on Web Designing Tips & Tricks by sending a blank email at email@example.com