I'm trying to fill a 2D Array initialized :
var cropField =
new Array(Math.floor((Math.random() * 9) + 1)).fill(new Array(Math.floor(Math.random() * 9) + 1).fill(" "));
Then, I traverse through the Array:
for (var i = 0; i < cropField.length; i++) {
row = cropField[i];
for (var j = 0; j < row.length; j++) {
cropField[i][j] = Math.random() > 0.5 ? "A" : "B";
}
}
I'm trying to fill the array with either A or B, randomly. But then this happens:
[ [ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ],
[ 'A', 'A', 'B', 'A', 'B', 'B', 'A', 'B' ] ]
They appear in columns.
I've tried using a forEach loop, that does the same thing.
Anyway I can get random values?