Help me understand this double for loop

This code is working…But i just not sure why i will get the result…

  1. When i use j<n instead of i<n, console.log(row) would give me this result:
    [0]
    [0, 0]
    [0, 0, 0]
    Doesn’t it should be stopped at [0, 0] since j would be equal to 2 after this and suppose to stopped? why would it continue to add another 0 into the row array?

  2. I cant figure out why i < n being used here. From what i understand doesn’t the code will keep looping instead of stopping after loop once?

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 = [];
  for (let i = 0; i < m; i++) {
    // Adds the m-th row into newArray
    
    for (let j = 0; i < n; j++) {
      // Pushes n zeroes into the current row to create the columns
      row.push(0);
      break;
    }
    // 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 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36.

Link to the challenge:

i dont know what you are trying to say but for your challenge you just need to reinitialize the row variable to an empty array after pushing it to newArray.

newArray.push(row);
    row = [];