I'm have a 2d array and I need to fill it with random values. But when I do it with a nested for loop, the values in each row are same. (example: [ [1,2,3] [1,2,3] ]
). This is my code.
let grid = new Array(10);
grid.fill(new Array(10));
for (let ri = 0; ri < 10; ri++) {
for (let ci = 0; ci < 10; ci++) {
grid[ri][ci] = Math.random();
}
}
// When I print this all arrays in the grid array are same
console.log(grid);
I need to know why is it and how to make all values random. Please help me. Thanks.