function zeroArray(m, n) {
let newArray = ;
let row = ;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
row.push(0);
}
newArray.push(row);
}
return newArray;
}let matrix = zeroArray(3, 2);
console.log(matrix);
In this code In the inner loop
1.first iteration will push 2 0’s[0,0].
2.Second iteration will push 2 0’s .so it become 4 . 2 from the 1st iteration and 2 from this iteration[0,0,0,0].
3.Third iteration will push 2 0’s .so it become 6 .2 from the 1st iteration and 2 from 2nd iteration and 2 from this iteration[0,0,0,0,0,0].
so the console should show [[0,0],[0,0,0,0],[0,0,0,0,0,0]] but it’s showing [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]].why?