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

4 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;
}

1 Like

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.

1 Like

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?

1 Like

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.

6 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.

2 Likes

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]

1 Like

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!

Hello, I passed with yet another solution but cannot understand why it is accepted. Here are the only changes that I made:

   // Pushes the current row, which now has n zeroes in it, to the array
    newArray.push(row);
    row = [];

If row is a global variable and I am passing references to it, then why is my newArray not reset to one containing several empty arrays? I get the correct answer instead:

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

I’m wondering the same thing!

1 Like

I used a different solution all together

function zeroArray(m, n) {
  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++) {
  
      row.push(0);
        
    }
    newArray.push(row);
       row = []
  }
  return newArray;
}

I reinitialized the row by reassigning the to the row in the outter loop

I will like to know if this is a good idea cause i kind of just repeated the n value in a diffrent location

I initially solved the problem, but I realised that I don’t understand it, I will just explain my thought process and ask a question after that:

before even “debugging” the code, I did not understand why are there 6 0s in every row, the way I understood it is that since row is initiated outside the for loop, in the function, then every time it is changed, it remains that way, so when filled up with 2 0s initially, pushed into newArray and then when row is filled up with another 2 0s and pushed again, row is changed globally, so the previously pushed row is also changed and now has 4 0s, and then 6, do I understand this correctly?

if that is the case, I understand why when setting the row = ; IN THE BEGINNING OF THE FIRST FOR LOOP, that it gets reset, every time the loop is run, and do I understand correctly, that it gets initiated first - then filled up with 2 0s the first time, pushed into new array and for now that would be the end, does that mean that when reset now, as the loop runs again, that the previous row pushed into newArray will become empty, until row is filled up again?

And lastly, if that last /first bit is true, why is it that, when the row variable is GLOBAL, and SET IN THE FUNCTION (like in the base code of the challenge) and if I put row = ; AT THE END of the first for loop, after pushing row into newArray, why does the code still work, instead of having the final thing done in the program being row = and so all pushed rows into newArray to = , creating 3 empty rows into newArray

PLEASE HELP ME I AM LOST

[Solution was redacted]

I’ve edited your code for readability. 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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

It’s great that you found a solution, but we don’t want to pollute the forum with solutions to challenges so I’ve wrapped your solution in [spoiler][/spoiler] tags.

exactly ! I even tried to imagine what I would do if I was a computer and I got the second output you told!