Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda