Tell us what’s happening:
so, I am not asking for the solution to this challenge, but I would like to know how the code is returning:
[ [ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ] ]

I know it has something to do with row being a global scope, but I still can’t figure out why the code is returning:
[ [ 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36 OPR/70.0.3728.178.

Challenge: Use Caution When Reinitializing Variables Inside a Loop

The suggestion I always give is to add console.log() statements to see the logic. Try watching the program when added like so:

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
console.log("m is " + m )
console.log("newArray is " + newArray
console.log("row is " + row)
for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
console.log("m is " + m )
console.log("newArray is " + newArray
console.log("row is " + row)
row.push(0);
console.log("m is " + m )
console.log("newArray is " + newArray
console.log("row is " + row)
}
// Pushes the current row, which now has n zeroes in it, to the array
newArray.push(row);
console.log("m is " + m ")
console.log("newArray is " + newArray
console.log("row is " + row)
}
return newArray;
}

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

Thanks for the reply! I still don’t fully understand the code, but hey we are getting somewhere. and thanks to your trick, I’ve gained a handy tool.

So continuing to what we were talking about, I’ve tinkered a bit with the code based on your suggestion:

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);
**console.log(row);**
}
// Pushes the current row, which now has n zeroes in it, to the array
newArray.push(row);
**console.log(newArray);**
}
return newArray;
}

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

The code above returns:

[ 0 ] < let’s focus on this
[ 0, 0 ] < and this. why is it returning [0,0] for the first loop test of j = 0. my mind is telling me it should return [0], and move on to the second loop of j = 1.
[ [ 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, 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 ] ]

this is pushing a reference to row

so newArray is first [row], then [row, row] and then [row, row, row]
and whatever is the value of row at that point is what’s inside newArray

you can see it happening if you do

let a = [0, 0, 0]
let b = []
b.push(a)
console.log(b) // [[0, 0, 0]]
a[1] = 1
console.log(b) // [[0, 1, 0]]
b.push(a)
console.log(b) // [[0, 1, 0], [0, 1, 0]]
1 Like

Is this something like:

zeroArray(3, 2)
for (let i = 0; i < m; i++)
for (let j = 0; j < 2; j++)

The first loop will create: [[], [], []]

Then, for the second loop:

j = 0, 0<2(true), row.push(0)  // row = [0], newArray.push(row) // newArray = [[0], [0], [0]]
j = 1, 1<2(true), row.push(0) // row = [0,0], newArray.push(row)/ newArray = [[0,0,0], [0,0,0], [0,0,0]]
j = 2, 2<2,(false)//row = [0,0,0], newArray.push(row)//newArray = [[0,0,0,0,0,0], [0,0,0,0,0,0 ], [0,0,0,0,0,0]

is this the right way to interpret it?

no, not really

try looking at it with this:

or try to look at the console using @themagicbean code

1 Like

Thanks!
Took some time, but managed to understand it