views:

21

answers:

2

I'm working on a really simple cellular automata program in JavaScript. For right now I just want to write a bunch of random Boolean values to a two-dimensional array and then read that array back to be manipulated or displayed in some way.

var dimension = 5;


var grid = new Array();


for (x = 0; x < dimension; x++) {

        grid[x] = new Array();

}


//populate grid

for (i = 0; i < dimension; i++) {

        document.write('<br>');
        for (j = 0; j < dimension; j++) {
           grid[i,j] = Math.floor(Math.random()*2);
           document.write(grid[i,j]);
 }
}

for (i = 0; i < dimension; i++) {

          document.write('<br>');
                  for (j = 0; j < dimension; j++) {
                  document.write(grid[i,j]);
    }
}

So, I've whittled the code down to a few nested for loops where I cycle through and populate the array and then print it back. Note that the output generated during the population loop is what I want, random values. But when I read the array back, it seems like the last row (I think it's really a column, but it's displayed horizontally) has been copied to all the others...

I've done this sort of thing before in other languages and never had a problem like this.

I'm new to this community and JavaScript in general so this might be a dumb questing or I may not have presented it helpfully. I would really appreciate any help or advice on how I can improve my question.

+1  A: 

Array indexes in JavaScript are not comma seperated. You need to use brackets. So for your two dimensional array it will be:

grid[i][j]; // not grid[i,j]
Jason McCreary
A: 

Thanks, Jason.

I guess it was a dumb question.

Brian Blackwell
This isn't an answer. You've also created a duplicate account. Go back to the same webbrowser/PC from where you've posted the question. Then you'll be able to mark his answer accepted and post a comment. Also consider registering your account there so that you will be able to login using the same account at every webbrowser/PC in the world.
BalusC