I'm currently working on a code that involves assigning a value to a single cell in a 2-D array. However, the assignment isn't giving me the expected result.
let arr = new Array(3).fill(new Array(3).fill(0));
arr[1][1] = 1;
console.log(arr.toString());
It's puzzling why the output turns out this way.
The second code snippet below gives me the desired output, but I prefer achieving it in a way similar to the first example.
let arr = [];
for(let i = 0; i < 3; i++){
arr.push([]);
for(let j = 0; j < 3; j++){
arr[i].push(0);
}
}
arr[1][1] = 1;
console.log(arr.toString());