Debugging - Use Caution When Reinitializing Variables Inside a Loop

I do not seem to understand the below statement.
Failed: zeroArray(4,3) should return an array holding 4 rows of 3 columns of zeroes each.
How do I go about this? Do i need to add another row?

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 = [0,0];
  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();
    }
    // 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);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Debugging - Use Caution When Reinitializing Variables Inside a Loop

Link to the challenge:

Did you see this line

Sometimes it’s necessary to save information, increment counters, or re-set variables within a loop.

What does matrix look like? What’s wrong with the rows? Is it a 3x2 array?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.