PDA

View Full Version : eval()uating for Arrays?


Eternity Angel
10-30-2002, 02:40 PM
I cannot figure how to do this, probably because I don't really know much about eval() for JS, although I know HOW it is supposed to work.
Anyways, I have Arrays that are called:

dirs1,
dirs2,
dirs3, and
dirs4

What I need to do, is make ANOTHER variable, which I am calling "dir", which contains the same value as one of those dirsX variables would. I have a random number between 1 and 4, and I am not sure how to make it so the "dir" variable will be almost identidal to the dirsX variables, by calling through dir[#], instead of dirsX[#].

I have no clue what I just said either, so, let me paste what I am trying to do, maybe IT will provide a better explaination.


var random = Math.floor(Math.random()*(5-1)+1);
var dir = eval("dirs"+random);
alert(dir[0]);


Is this even possible? I get an "undefined" when I alert() the value of dir, and an "dr.1 is undefined" if I alert() dir[0].

If you didn't understand the question I had in there, then, MAYBE I could try to repeat it...

beetle
10-30-2002, 02:55 PM
I tried this and it works fine...var dirs1 = ['a','e','i','m'];
var dirs2 = ['b','f','j','n'];
var dirs3 = ['c','g','k','o'];
var dirs4 = ['d','h','l','p'];
var random = Math.floor(Math.random()*(5-1)+1);
alert(random);
var dir = eval("dirs"+random);
alert(dir[0]);

Eternity Angel
10-30-2002, 03:01 PM
I am sorry. It was merely a typo on my end...

Thanks very much anyhow, for making me see it ;)