while practicing with loops I receive kind of weird to me result.
let a = [];
let b = [];
for (let i=0; i<2; i++){
for (let j=0; j<2;j++){
a.push(0);
}
b.push(a);
console.log(a);
console.log(b);
}
Output looks like this:
//values of arrays after first iteration of outside loop
[ 0, 0 ]
[ [ 0, 0 ] ]
//values of arrays after second...
[ 0, 0, 0, 0 ]
[ [ 0, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
I wonder why value of array b is not equal to [ [0, 0], [0, 0, 0, 0] ]?
a, so the same array is pushed ontobeach time through the outer loop.b.push([...a]);to get the output you expect.