Quote:
Originally Posted by Mr-Moo
Just tossing the question out there because I am interested in learning, but are you able to assign multiple results to a variable? If so, why and what are the circumstances?
Thank you!
|
Yeah, it's called an array. You would do it to keep lists of things (like pictures, names). That's what we're doing with the
[i] stuff at the end of the variable name.
So like...
Code:
var friends = new Array();
friends[0] = "Woody";
friends[1] = "Buzz";
friends[2] = "Potato Head";
friends[3] = "Rex";
friends[4] = "Hamm";
friends[0] = "Slinky";
friends[6] = "Bo Peep";
Will be multiple variables, multiple strings (names) of people in the variable
friends.
You can also use 2D arrays to make arrays of arrays. This is to keep track of checkerboard kind of things.
Code:
var timestable = new Array(3);
for(var i=0; i<6; i++){
timestable[i] = new Array(5)
}
for(var i=0; i<timestable.length; i++
{
for(var j=0; j<timestable[0].length; j++)
{
timestable[i][j] = i * j;
}
}
document.writeln("2 x 3 = " + timestable[2][3]);
The code above makes a multiplication table, that looks something like this...
Code:
0 1 2
0 [0][0][0]
1 [0][1][2]
2 [0][2][4]
3 [0][3][6]
4 [0][4][8]
The first subvariable calls the column, the second subvariable calls the row. So
timestable[1][4] is 4. Then
timestable[2][2] is also 4.
So MarkFoster here can solve his problem either with a two arrays or a 2D array. Either way, he has to extensively change his code. Because the list probably isn't very long, and he would only need two rows - using a 2D array would be wasteful and inefficient. He probably needs to just keep two arrays (which is what I've given him code for).