I am not getting this output

The challenge focuses on the fact that the row array is not being reinitialized, but I don’t see what that has to do with the first index having six zeroes which is the output it gives for the below code. The first time it runs through when it hits the inner loop it runs from 0 to 1, bc it’s going up to one less than n which is equal to 2. Thus, after exiting the loop it should push an array with two zeroes to the first index of new Array at least for the first time through. What am I missing?

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
    // Pushes the current row, which now has n zeroes in it, to the array
  return newArray;

let matrix = zeroArray(3, 2);

Please post a link to the challenge too so it’d be easier to understand the problem

You’ll find your answer here. Array references can be tricky to understand that it tricked me even in my very last challenge/project of this certification which I completed yesterday. Even though I asked the same question myself some time ago, I was confused initially but saw the answer and it was clear. This person explains it clearly with an image too. Let me know if you have any further questions/confusion and I’ll try to answer or we can tag him if I can’t answer.

Click this link. He explains it very well here. :slightly_smiling_face:

What @ankurchaulagain linked to describes whats going on pretty well so I won’t talk on the specific subject too much, but I will speak on something a bit more fundamental.

In programming you have Pass by Value and Pass by Reference, and in JavaScript objects, and arrays are both Pass by Reference whereas primitives such as numbers are Pass by Value.

To be Pass by Value means that when I say let a = 1 and then say let b = a, b gets it own copy of 1 so the actual value was passed which means if I were to later say b = 2 this would have no effect on a as they point at their own values.

Objects can hold an arbitrary amount of elements so they can be quite expensive in terms of memory so we don’t want to make a new copy every time someone says:

const a = new Array(10000000000).fill(1);
//this array has a lot elements!
//to be clear the elements don't matter, but it would be
//even more expensive to fill it with objects that also
//have elements, which is exactly why this:
const a1 = a;
//points at the same array in memory rather than creating a
//deep copy of it
1 Like

Ok thanks, but wouldn’t a = 2 be a more fitting example just because you originally said let b = a ?

They both point at there own 1(s) and neither of the variable names have a special meaning so I feel the distinction is arbitrary as too wether I say a = 2 or b = 2.

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