Curriculum array output problem

Tell us what’s happening:
Why output is:
[ [ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ] ]
[ [ 0, 0 ],
[ 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ] ]

``````
function zeroArray(m, n) {
// Creates a 2-D array with m rows and n columns of zeroes
let newArray = [];
let row = [];
for (let i = 0; i < m; i++) {
// Adds the m-th row into newArray

for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
row.push(0);

}
// Pushes the current row, which now has n zeroes in it, to the array
newArray.push(row);
}
return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0`.

Challenge: Use Caution When Reinitializing Variables Inside a Loop

here a reference to `row` is added to `newArray`, not a copy
so at the end you have
`[ row, row, row ]`, whatever the `row` array looks like

i still don’t understand, when i = 0 I am only pushing 2 zeros to newArray, when i = 1 I am pushing 4 zeros…

it means that when i = 1 and I use newArray.push(row), I am updating row that I pushed when i was equal to 0?

try following this:

``````let arr = [0,0,0]
let outerArr = [];

outerArr.push(arr)
// outerArr equals [[0,0,0]]

arr[1] = 1;
// arr is equal [0,1,0]
// and outerArr is also equal to [[0,1,0]]
outerArr.push(arr)
// now outerArr is equal [[0,1,0], [0,1,0]]
``````

because arrays work by reference, doing `outerArr.push(arr)` you are pushing a reference to `arr`, aby change to `arr` is reflected in `outerArr` because `outerArr` is practically `[arr, arr]`