0

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(JSON.stringify(matrix));

this code should print this

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

but instead its printing this

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

can someone trace the code?

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • What do you mean "trace the code"? – asportnoy Aug 31 '22 at 23:09
  • Write to the [console](https://developer.mozilla.org/en-US/docs/Web/API/console) what you need to see in the loop to understand what the loop is doing. – Robert Harvey Aug 31 '22 at 23:11
  • What you're probably looking for is something akin to `for (let j = 0; j < i; j++)` in your second loop. Notice the condition is checking against i instead of n? – Robert Harvey Aug 31 '22 at 23:12
  • 2
    You are using only 1 row array because you do `let row = [];` only once. That single row array is pushed multiple times into to `newArray` variable. At the same time you also push new `0`s into that single row array. The single row array has 6 `0`s at the end, and newArray contains that single row array 3 times so it prints 3 times the 6 `0`s. – Peter B Aug 31 '22 at 23:15
  • any browser have a step by step debugger (**F12**) – Mister Jojo Aug 31 '22 at 23:16
  • When you `push` `row`, you are not pushing the *contents* of the row array, you are pushing a *reference* to the row array. As the row array changes through the loops, the values in the reference are updating. See: https://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array-without-creating/17368101#17368101 and https://stackoverflow.com/questions/4156101/copy-array-items-into-another-array – Jonathan Aug 31 '22 at 23:18

3 Answers3

0

I think this is about variable scope.you should declare row in for loop

AlanZh
  • 3
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 06 '22 at 07:00
0

When you push row, you are not pushing the contents of the row array, you are pushing a reference to the row array. As the row array changes through the loops, the values in the reference are updating.

If you slice() the row array when you push it into the new array, you will copy the values of the array, instead of a reference to it:

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.slice());
  }
  return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);

output:

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

See:

Copy array by value

Jonathan
  • 4,916
  • 2
  • 20
  • 37
0

a solution...

function zeroArray(m, n)
  {
  let newArray = new Array(m)
    , count    = n
    ;      
  for (let i = 0; i < m; i++, count += n)
    newArray[i] = new Array(count).fill(0)
    ;
  return newArray
  }

let matrix = zeroArray(3, 2);

console.log(JSON.stringify(matrix));
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40