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

248
Visualizações
JavaScript for loop, I don't understand

I don't understand the output of this question from CodeAcademy. It is stated in the for loop that X+=1, but why is the output printing the X index 0 for the second time? I thought it would be index 1(A) as X+=1.

const tags = ['G', 'A', 'T',' C'];
for (let x = 0; x < tags.length; x +=1) {
  for(let y = 1; y < tags.length - 1; y +=1) {
    console.log(`${tags[x]}${tags[y]}`,x,y);
  }
}
//output GA  GT  AA  AT  TA  TT  CA  CT

Output:

Console Image

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The inner loop starts with y = 1 and ends with y = 2 for this tags array. That means for each iteration of the outer loop, the console.log runs twice. That's why you are seeing 4 * 2 = 8 outputs printed out, where 4 is the number of iterations the outer loop goes through, and 2 is the number of iterations the inner loop goes through each time.

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