Debugging how to fix zeros in the output

Debugging how to fix zeros in the output
0

#1

Hello,
I get this one
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
].
instead of
[0, 0],
[0, 0],
[0, 0]
]
I dont know how to fix in order to get this, even if the fcc says that i succeeded, because i paste let row in the for loop.


#2

What is the challenge url you are working on? Also, please copy/paste your current code.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

The challenge is this one : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/use-caution-when-reinitializing-variables-inside-a-loop/

my code is this (which pass the challenge ) :

function zeroArray(m, n) {
  // Creates a 2-D array with m rows and n columns of zeroes
  let newArray = [];
 
  for (let i = 0; i < m; i++) {
    // Adds the m-th row into newArray
     let row = [];
    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);

but in yhe output i dont get
[0, 0],
[0, 0],
[0, 0]
]


#4
[ [ 0, 0 ], [ 0, 0 ], [ 0, 0 ] ]

The above is the same as the following:

[
 [0, 0], 
 [0, 0],
 [0, 0]
]

OR

[[0, 0],
[0, 0],
[0, 0]
]]

#5

I didn’t get it but this one [0, 0, 0, 0, 0, 0] :confused:


#6

The code you posted will not display [0, 0, 0, 0, 0, 0] to the browsers console. If you are seeing [0, 0, 0, 0, 0, 0] in the console, then you must be running different code. See the console below after running your code. Also, I posted the same code as it would run on node.js and you will see the correct output shown. Please post your browser’s console showing the [0, 0, 0, 0, 0, 0] if you don’t mind.


#7

Ok thank you :smiley: