Tell us what’s happening:
Describe your issue in detail here.

``````  **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; 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/91.0.4472.114 Safari/537.36`

Challenge: Use Caution When Reinitializing Variables Inside a Loop

`row` variable needs to be reinitialized during each iteration of the outer loop. Your `row[]` array is not getting reset after each iteration here. Therefore, you will not get the expected output.

1 Like

When you start working on a problem, the first thing is to look at the input and the output of the function that you are supposed to write.

In this case:
Your input is 2 variables namely m and n and for a test case suppose

``````let  m = 3;
let  n = 2
``````

The output that you are going to generate is 2 dimensional array which we can call result.

``````return result
``````

The problem asked you to create a 2 dimensional array with m rows and n columns.

which roughly translates to

For each m you need to do n times of something.

Now to implement this in js we need to use a nested for loop

``````for( let i = 0; i < m;  i++){
//do something for each m

for(let j = 0; j < n; j++){
//do something for each n

}
}
``````

the outer loop will help us create our rows and the inner loop will help us put data into our rows.

Now, inside the outer loop, we create an array which acts as our row and on each iteration of the inner loop we push some data, in this case 0 onto it.

``````  let row =[]
for (let j = 0; j < n; j++) {
row.push(0);
}
``````

after it’s done iterating n number of times and has created the row variable which contains n number of zeros. Which in the case of our test case would be

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

the control will go back to the outer loop and the row variable is pushed onto the result array. after which the value of the result variable would be

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

this process is repeated m number of times. hence the sentence

For each m you need to do n times of something.

and after each iteration of the outer loop, this row variable is reset to an empty array. and finally we can return the result array as the output.

``````  result.push(row);
``````

and the result array would be

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

I got the idea of i and j here.But i am still confused that why output difffers when we define row= before the for loop and when we define row= just after outer loop.
We get correct answer after we declare row = just after outer looop

Hi,

the above explanations look great but to smoothen the rollercoaster a little:
i console.log everything(before, after in between… it’s the only way i can make sure there’s no evil spirit messing with my code (and i always come to believe in him) and i set up break points so i can step through the code and see exactly what happens. this means you copy the code into a local editor inside an html file and use your dev tools inside a browser.

I would be lost without breakpoints.

greets,
Karin

the empty array outside the for loops create the outer array which is supposed to hold your rows.

this array holds individual values inside each row.

the empty array inside the outer loop is reset after each iteration which means unless you have some place to store those values, there’s no point in doing this exercise as it would just reset thats why we need the array before the outer loop.

The problem is the row is not cleared on each loop.

Try to see the flow below. This is not real code, imagine I wrote that on notepad while following the flow of the code:

``````newArray = []
row = []
row = [0]
row = [0, 0]
newArray = [[0,0]] // we put the row inside newArray

row = [0,0,0] // oh no! we didnt put the row empty again!
row = [0,0,0, 0]
newArray = [[0,0,0,0], [0,0,0,0]] // add row

row=[0,0,0,0,0]
row=[0,0,0,0,0,0]
newArray =[[0,0,0,0,0,0], [0,0,0,0,0,0], [0,0,0,0,0,0]]
``````

Now if you put the row back to every time, you are clearing the previous reference:

``````newArray = []
row = []
row = [0]
row = [0, 0]
newArray = [[0,0]] // we put the row inside newArray

row=[] // we clear the row!
row = [0]
row = [0, 0]
newArray = [[0,0, [0,0]]  // we add the row

row=[] // we clear the row!
row = [0]
row = [0, 0]
newArray = [[0,0, [0,0], [0,0]]  // we add the row
``````

Try to go through the code and see what is happening at each step. You may need to think about it a bit.

I solved the problem in a different way because loops inside of loops tend to be hard to read and tend to be slow. This is what I did:

``````function zeroArray(m, n) {
// Creates a 2-D array with m rows and n columns of zeroes
let newArray = [];
let row = [];

for (let j = 0; j < n; j++) {
// Pushes n zeroes into the current row to create the columns
row.push(0);
}

for (let i = 0; i < m; i++) {
// Adds the m-th row into newArray
// 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);
``````

Hope this helps

2 Likes

Thank you @arturcarvalho you really simplified the loops and i got this.
I should think like you to solve a problem through various ways like you just did and it is much simple to understand.
Have a great day!

1 Like