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

153
Visualizações
My for loop runs only once while trying to reverse an array in javascript

var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
  list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);

I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.

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

0

As @Barmar pointed out the problem is due to a logic error.

/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
  for(let i = 0 ; i < size ; ++i)
    list.push(window.prompt());
}

/* Print a list container */
function print(list){
   for(let i = 0 ; i < list.length ; ++i)
    console.log(list[i]);
}

var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);

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