What do you think about this solution ? JavaScript Course

I try a different solution as show in Use Caution When Reinitializing Variables Inside a Loop. I try next code:

    for (let j = 0; j < n; j++) {
      // Pushes n zeroes into the current row to create the columns
      row[j]=0;
    }
    // Pushes the current row, which now has n zeroes in it, to the array

I want to know if my solution is validate for this excersie.

Thanks everyone!!!

Sure, if it works, it works.

I mean, I don’t know if it is in the spirit of the challenge, but it does work, and isn’t really breaking any of the rules. But the description talks about its placement being the problem and the accepted solution fixes the placement. Yours does not.

I would say that you are just re-writing over the same elements on each pass so it’s wasteful, but then again, the suggested answer doesn’t make a lot of sense to me either.

I would also point out that the accepted answer also fixes another potential problem - all the rows having the same reference. Yours does not address that. (Though there may be fringe cases where this is desired.)

3 Likes

Yeah I also sort of did an out of the box solution and only then checked that moving the row array into the loop was the solution.

Instead I looked at the code and simply moved the inner for loop outside because it did not have to be there to achieve the desired result…

function zeroArray(m, n) {
let newArray = [ ];
let row = [ ];
for (let j = 0; j < n; j++) {
row.push(0);
}
for (let i = 0; i < m; i++) {
newArray.push(row);
}
return newArray;
}

This misses the idea of the exercise. You have m references to the exact same row, so this isn’t really an array of different arrays.

Now I do completely agree I have missed the point of the exercise, I knew that the same moment I checked out the solution after passing the exercise test.

However I am completely missing what you are trying to tell me by:

The exercise used an array ‘row’ containing n number of ‘0’ and pushed the array into another array ‘newArray’ m number of times. It really feels like the same array pushed into another arrays m times. And you pointed out that it:

what difference does the fact that they are the same make?

In JS a reference variable (like objects, arrays, functions, etc.) are “references” or “memory addresses”. When you copy them (in a simple way), you are just coping the address. If I write down my house address on 10 pieces of paper, it is not making 10 houses, it is just 10 references to the same house. In your code, you are creating a array (row) and copying that address m number of times. You are not creating m number of row arrays, but m copies of a reference to the same array.

what difference does the fact that they are the same make?

A big one. If I am Oprah, giving out a new house to 10 needy families, there is a big difference if all the address cards point to the same house or to different houses.

With regards to programming, with your code, any change I make to any row will also change all the others, because there is only one row array - you’ve just copied the address over and over. Just like with the houses - if they addresses are all the same, if I paint one house blue, they all are painted blue because they are all the same house.

4 Likes

Thank you KevinSmith, again.

It would makes sense in a case where you wouldn’t just print out ‘0’, now I see what JeremyLT meant after you explained in detail.

1 Like

It would if you wanted to change a cell. Maybe you’re just initializing your grid with zeroes. Try it out and change on of the cells and see how it affects the whole grid.

I’m not sure if I understand why we are initially getting to a matrix of:
[000000]
[000000]
[000000]
As two 0’s get added to the row variable each time through the outer loop - wouldn’t we get to the following matrix?
[00]
[0000]
[000000]

Its because arrays are a reference and not a completely new value like @kevinSmith was saying earlier. You are adding two zeros with the inner loop, but all three rows are actually pointing to the same exact thing. So you end up with three rows with six 0’s in the end.

Put another way, if you understand this code, you will see the issue:

const arr1 = [1]
const arr2 = arr1

console.log(arr1)
// [1]
console.log(arr2)
// [1]

arr1[0] = 42

console.log(arr1)
// [42]
console.log(arr2)
// [42]
2 Likes

Thanks heaps for your response Kevin. Really wrapped myself around the axle on that one. Cheers!