Re initializing Variables Inside a Loop

Re initializing Variables Inside a Loop
0

#1

Tell us what’s happening:

I can’t fix this problem. I don’t really understand what kind of changes should be in matrix variable.

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];
  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);
    }
   
    // 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/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/use-caution-when-reinitializing-variables-inside-a-loop


#2

If you open the developer console (Ctrl-Shift-I in Chrome) and run the tests, you’ll see

2018-08-27_9-06-23

in the console. Notice that the each of the three arrays created have six elements instead of two. What variable would you need to re-initialize (i.e., make empty) to have only two elements for each array each time, as shown below?

2018-08-27_9-11-51


#3

Removed screenshot of ES6 solution.

This is my first post. Forgive me if I confused matters with my screenshot.

So after clicking on the link provided…might I suggest breaking the problem into 2 parts.
First, make a single row array produce the required number of 0 elements.
Then, embed the solution to the first step into another iterative loop to replicate the single row solution across multiple rows of the array.

For more information you may want to peruse MDN.

Hope this helps!
Rowen