Debugging: Use Caution When Reinitializing Variables Inside a Loop

Debugging: Use Caution When Reinitializing Variables Inside a Loop
0

#1

I tried a lot ,but I still can not understand why what I have done here is not working as an approach of this challenge .

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
newArray.push(row);//here everytime I push a new arr (row) to newArray array
for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
newArray[i].push(0); //here newArray[i]=newArray[0], and in the second time it is gonna be newArray[i]=newArray[1];

}
// Pushes the current row, which now has n zeroes in it, to the array
  newArray.push(newArray[i]);//here I push the last newArray[i] that I have just pushed to it two 0's
  // the resutl [Array(6), Array(6), Array(6), Array(6), Array(6), Array(6)]

}
return newArray;// here the resut is different I dont know why
}

let matrix = zeroArray(3, 2);
console.log(matrix);

By the way, I can solve the problem using another method but I wanted to follow what they said to me to do in the comments.
I 'll apperciate any help ,thanks in advance.


#2

i think i see what the problem is, but it would help if you posted the link to the challenge.


#3

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


#4

OK, so here is the original code:

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; 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);

and the instructions (i bolded the most important part):

The following function is supposed to create a two-dimensional array with m rows and n columns of zeroes. Unfortunately, it’s not producing the expected output because the row variable isn’t being reinitialized (set back to an empty array) in the outer loop. Fix the code so it returns a correct 3x2 array of zeroes, which looks like [[0, 0], [0, 0], [0, 0]].

so, the inner loop is for…j and the outer loop is for…i
at the end of the outer loop, before the outer loop is closed, the row variable needs to be set back to zero

i hope this helps.


#5

also, just a tip for posting code in the forums so people can read it:

When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

(h/t @ArielLeslie)


#6

thanks megantaylor for this help

for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
row.push(0);
}
/* here row is adding 0’s to the same array, I thought if I replace it by newArray[i] that will solve the issue /*

I am still confused but ,I have passed the challenge using loop by another way .


#7

The row needs to be reinitialized/declared in a different part of the code. There’s actually no “writing” involved. Just the movement of a piece of the code.


#8

thank you tanhuy this helps , unfotunately the comments misleaded me , I considered them as instructions ,
Now I figured out that I should put let row=[] in the first loop to reinitialize the row variable.
thank you all campers.


#9

Not a problem. I’m learning as well and the wording at times can be difficult.


#10

Hi, and for all of you who are searching for a final solution, this is the right on.
Keyword is: move the empty array declaration inside the outer for loop

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);

All the best and keep on coding