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);
}
return newArray;
}
let matrix = zeroArray(3, 2);
console.log(matrix);
In this code I'm to reinitialize the row variable in the outer loop to an empty array inorder to create a 2D array with rows and columns that look like this: [[0,0], [0,0], [0,0]]. Without reinitializing the variable I get a completely different outcome. I understand how the code works but I dont understand the full impact of the reinitializing. Why would setting the row variable back to an empty array create a different ouptut?