Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda