# Help me clarifyng this

Tell us what’s happening:
Hi,
I understand where the error in the challenge code is (maybe I don’t understand it completely), but I can’t figure out why `zeroArray(3, 2)` returns:

``````[[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], [0, 0, 0, 0, 0, 0]]``````

I would expect:

``[ [ 0, 0], [0, 0, 0, 0], [0, 0, 0, 0, 0, 0] ]``
as at the end of each inner loop iteration, `row` is equal to [0, 0] , then [0, 0, 0, 0] and then [0, 0, 0, 0, 0, 0].
Let’s consider the end of the inner loop first iteration: `row` is now `[0, 0]`, and it gets pushed into `newArray`. Even if `row` is global and its value doesn’t get initialized at every inner loop, why at the end of the function `newArray[0] == [0, 0, 0, 0, 0, 0]` and not `[0, 0]` ? I don’t get it.
Thank you

Your code so far

``````
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 (Macintosh; Intel Mac OS X 10.11; rv:78.0) Gecko/20100101 Firefox/78.0`.

Challenge: Use Caution When Reinitializing Variables Inside a Loop

Link to the challenge:

because what you pushed to `newArray` was not a copy of `row`, but a reference to it, so each change to `row` is reflected to `newArray[0]`and viceversa

1 Like

I think I get it now…in other words it’s because at the end of the function newArray == [row, row, row] , right?

yes, correct!

1 Like

Thank you, ieahleen, that has been helpful