...

View Full Version : Using for loop to count through variables?



weirdwes
03-10-2004, 08:11 AM
I searched the forums and google and I kept ending up at irrelevant pages. Perhaps I wasn't searching for the correct term.

Imagine I have a set of variables (Item1 - Item10) and I want to do something with their values. (anything really). How can I set up a FOR LOOP that will count from 1-10, add that as "string" to the end of "item" and then do my thing. Yes this would be easier with arrays, but it has others uses, I just need to know how to do it. :D

The way I do it now (yikes)



Var item1 = "";
Var item2 = "";
Var item3 = "";


(etc)

How can I do it like this?



For (X=1; X <= 10; X++;) {
Var item+x = "";
}


This is probably really easy. :p
Thanks!

Garadon
03-10-2004, 08:23 AM
For (X=1; X <= 10; X++ {
window['item'+x] = "";
}

weirdwes
03-10-2004, 08:33 AM
Thank you, that does trick!

glenngv
03-10-2004, 09:26 AM
More info on that trick here (http://www.litotes.demon.co.uk/js_info/sq_brackets.html).

weirdwes
03-10-2004, 04:49 PM
Thanks for that link. It's nice to know why something works instead of just using it not knowing what it does. Much appreciated.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum