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

147
Visualizações
For loop only returns last number

I have made a for loop in order to console log multiple entries in an array. The for loop, however, only returns the last entry in the array, instead of everything from 0 to end of array.

for (var i = 0; i < roa.length; i++) {questionContentRoa = roa[i].questionContent, correctAnswerRoa = roa[i].correctAnswer }
                console.log(questionContentRoa, correctAnswerRoa);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It will be clearer to you if you ident the code a little bit.

The console.log is outside of the scope, hence, it's only logging the last assignment before the loop ends.

for (var i = 0; i < roa.length; i++) {
    questionContentRoa = roa[i].questionContent;
    correctAnswerRoa = roa[i].correctAnswer;
}
console.log(questionContentRoa, correctAnswerRoa);
about 4 years ago · Juan Pablo Isaza Relatório

0

The console.log should be inside the for loop. You have placed it outside.

Alternatively, an easier way to iterate through an array is by using the forEach loop.

roa.forEach((item)=>{
console.log(item.questionContent, item.correctAnswer );
}); 

// in the first iteration item would be the roa[0], 
//then in the second iteration it would be roa[1] and so on until the array ends..

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