Answers for "Reinitializing variables inside a loop"

0

Reinitializing variables inside a loop

function zeroArray(m, n) {
  let newArray = [];
  for (let i = 0; i < m; i++) {
    let row = []; /* <-----  row has been declared inside the outer loop. 
     Now a new row will be initialised during each iteration of the outer loop allowing 
     for the desired matrix. */
    for (let j = 0; j < n; j++) {
      row.push(0);
    }
    newArray.push(row);
  }
  return newArray;
}
let matrix = zeroArray(3, 2);
console.log(matrix);
Posted by: Guest on April-21-2021

Code answers related to "Reinitializing variables inside a loop"

Browse Popular Code Answers by Language