Click to See Complete Forum and Search --> : Help-a-newbie with variables ...
w_conti
03-10-2003, 12:41 PM
Greetings Javascripters,
Need to build a series of variables on the fly:
for(var i=1;i<=24;i++){
'xxx'+i = ....
}
This doesn't work.
Please advise.
Try this:
for(i=1;i<=24;i++)
{
xxx[i] = ....
}
AdamBrill
03-10-2003, 02:22 PM
One thing is you must create the array first. Like this:
xxx = new Array();
for(i=1;i<=24;i++)
{
xxx[i] = ....
}
Then it should work. I hope that helps... ;)
Nedals
03-10-2003, 02:31 PM
for(var i=1;i<=24;i++){
'xxx'+i = ....
}
A few points on your snippet of code.
You are not really creating variables on the fly. You are attempting to use an array and it's a good idea to declare the array
var xxx = new Array(); ( ;'s are optional in javascript but with other languages they are not. So get into the habit of including them.)
Note that the first item in the array is always 'xxx[0]' so the 'for' loop is better written..
var xxx = new Array();
for (var i=0; i<24; i++) {
xxx[i] = ....;
}
And that my mini tutorial for today! :)