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

199
Visualizações
for loop to multiply index 0 with 1, index 1 with 2, index 2 with 3

how could i generate a loop that iterates through a array, and its just multiplying the next index?

example:

array= [0,1,2,3,4,5]

0x1
1x2
2x3
3x4
4x5 

i know that i need to have a nested loop, but I cannot find the solution.


  for (let i = 0; i < Positions.length; i++) {
    for (let j = 1; j < Positions.length; j++) {
    
      
      console.log(Positions[i].number * Positions[j].number  )  
 
    }
    
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem with your current implementation is that you are looping over every index twice: once for the outer loop and once for the inner loop.

To get the item at the index after i you can simply write Positions[i + 1].

You should loop over every index once like this:

for(let i = 0; i < Positions.length - 1; i++) {
    console.log(Positions[i] * Positions[i + 1])
}
about 4 years ago · Juan Pablo Isaza Relatório

0

let arrayValues = [0, 1, 2, 3, 4, 5];

arrayValues.forEach((value, index) => {
  if (index !== (arrayValues.length - 1)) {
    console.log(`${value} * ${index + 1}  = ${value * (index + 1)}`)
  }
})

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