I don't get this. Help

I know the answer but just don’t get what and why is this happening.

  **Your code so far**

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

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36

Challenge: Use Caution When Reinitializing Variables Inside a Loop

Link to the challenge:

It is not clear as to what your question is or what you don’t understand.

Hi @Pioneer369 !

I added spoiler tags around your code since this is a working solution.

I am also confused as to what you need help with.

If you were able to come up with the answer, why you are confused by your own code?

If you could expand on what you need help with that would be good :grinning:

The for loop seems to be your problem I assume.

Your first for-loop creates a row (simply an array) on the 1st iteration.

Before the nested for-loop executes, your newArray looks like this:

newArray = [
    [ ]
]

When the nested for-loop executes once, our newArray looks like this:

newArray = [
    [ 0, ]
]

And however many times you like it to execute your nested for-loop will execute and add a 0.

newArray = [
    [ 0, 0, 0, 0....]
]

Then your parent for-loop will execute again and add another array:

newArray = [
    [ 0, 0, 0, 0....],
    []
]

and your nested for-loop will add zeros however many times you like:

newArray = [
    [ 0, 0, 0, 0....],
    [ 0, 0, 0, 0....]
]

And your parent for-loop will execute as many times as you want as well, therefore:

newArray = [
    [ 0, 0, 0, 0....],
    [ 0, 0, 0, 0....]
    .
    .
    .
]

I hope this helps you in some way.

PS - Here is a video that might help:

1 Like

I am confused by the point of challenge. what is the difference for that array to being inside or outside of that for loop

thanks man that helped a lot but I want to know what does happen to the row array.
when it is outside of nested loop and when it is inside. does it reset when it is inside?

You are asking about the row array, right?

When the array is inside the outer loop it will be re-declared and re-initialized to an empty array whenever the outer loop runs. So for each iteration of the outer loop, you set let row = [].

If the array is declared outside the loop it is only declared and initialized one time when the function runs.

1 Like

Because sometimes you will want to run code inside a nested array.

function findMe(arr) {
  //create an emtpy array to store your integers.
  var yourArr = [];

  for (let x = 0; x < arr.length; x++) {
    var largestNum = arr[x][0]; //initialize a var called largestNum as a default nested array, starting from index 0 inside a loop.
    for (let y = 0; y < arr[x].length; y++) {
      if (arr[x][y] > largestNum) {
        largestNum = arr[x][y]; // reinitialize largestNum to hold the largest numbers.
      }
    }
    yourArr[x] = largestNum //make sure to give [x] to yourArr so it doesn't become empty.
  }
  return yourArr;
}

console.log(findMe(
  [
    [0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9, 10, 11]
  ]
));

//prints [ 3, 7, 11 ] in the console.

You could also take this a step further with even more nested for loops but you got higher order functions like map(), reduce() etc. for that, which make these operations so much easier. Makes the code better too.

1 Like

yea that’s exactly what I wanted to know. why is this happening?

You mean the reason for why we have inner loops in the first place?

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