Help with multidimensional arrays


#1

Where am I wrong? I have the following code to populate 2-dimensional array. Both dimensions should have length of 25 each, with numbers from 1 to 625 (25*25):

var gridSize = [25,25];
var grid = new Array();
for (i0=0;i0<gridSize[0];i0++) {
    for (i1=0;i1<gridSize[1];i1++) {
        grid[[i0],[i1]] = (i0*25)+(i1+1);
    }
}

But my output looks like this (I just copied x0 and x1, but that pattern repeats for all x 0-24, with numbers from 601 to 625):
x,y:value
0,0:601
0,1:602
0,2:603
0,3:604
0,4:605
0,5:606
0,6:607
0,7:608
0,8:609
0,9:610
0,10:611
0,11:612
0,12:613
0,13:614
0,14:615
0,15:616
0,16:617
0,17:618
0,18:619
0,19:620
0,20:621
0,21:622
0,22:623
0,23:624
0,24:625
1,0:601
1,1:602
1,2:603
1,3:604
1,4:605
1,5:606
1,6:607
1,7:608
1,8:609
1,9:610
1,10:611
1,11:612
1,12:613
1,13:614
1,14:615
1,15:616
1,16:617
1,17:618
1,18:619
1,19:620
1,20:621
1,21:622
1,22:623
1,23:624
1,24:625


#2

Remove the comma. These are both subscripts. Also,

    grid[i0][i1]

I would recommend using two letters that are easy to distiguish, like p and q, u and v, etc.

var gridSize = [25, 25];
grid = [];
var p, q;
for (p = 0; p < gridSize[0]; p++) {
    grid[p] = [];
    for (q = 0; q < gridSize[1]; q++) {
        grid[p][q] = p * 25 + q + 1;
    }
}

#3

Hmm, when I remove the coma and make something like:
grid[x][y] = (x*25)+(y+1);
I get "undefined is not an object" error...


#4

Notice how I defined each row as containing an array?


#5

Oh I see, think I got the JS "multidimensional" arrays now... Thanks!