Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Reinitilize Variable Inside a loop. Help me understand its impact in the code below

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);
      console.log(row);
    }
    // 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);

In this code I'm to reinitialize the row variable in the outer loop to an empty array inorder to create a 2D array with rows and columns that look like this: [[0,0], [0,0], [0,0]]. Without reinitializing the variable I get a completely different outcome. I understand how the code works but I dont understand the full impact of the reinitializing. Why would setting the row variable back to an empty array create a different ouptut?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!