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

102
Visualizações
My browser lags when I try a loop function?

I wrote a simple nested loop function to multiply all items in an array and output the total value, but each time is I run a loop function my browser either crashes or doesn't stop loading

function multiplyAll(arr){ 
        Let product = 1;
       for(let i = 0; i < 
            arr.length; i++){
            for(let j = 0; j < 
     arr[i].length; j *= product);
     }
      return product;
}

multiplyAll([[1], [2], [3]]);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are creating an infinite loop here because of

for (let j = 0; j < arr[i].length; j *= product);

Here, j is always 0.

If you want to multiply all the nested value then you should do as:

function multiplyAll(arr) {
  let product = 1;
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; ++j)
      product *= arr[i][j];
  }
  return product;
}

console.log(multiplyAll([[1], [2], [3]]));


If you just want to multiple all the nested value then you can simply do as:

function multiplyAll(arr) {
  return arr.flat().reduce((acc, curr) => acc * curr, 1);
  // If you want to get numbers at any depth then you can flat at any level
  // using Infinity as
  // return arr.flat(Infinity).reduce((acc, curr) => acc * curr, 1);
}

console.log(multiplyAll([[1], [2], [3]]));

about 4 years ago · Juan Pablo Isaza Relatório

0

Well, it looks like you have multiple issues, two of them are already as following:

  1. In line 2 you wrote once Let with a big letter at the beginning.
  2. Your second array needs an {} otherwise it will error out
  3. Can you please explain to me j*= product? Can you maybe include a few examples of how you would call this Code and what kind of response you would get? depending on that I will create you a Clear answer!
about 4 years ago · Juan Pablo Isaza Relatório
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