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

243
Vistas
Why when I pass "q" the second time into my input it does not break the loop?

It works the first time when I pass "q" into my input, but after that, it no longer works. Why? Appreciate any kind advice.

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let toDo = [];

while (userInput != "q") {
    let userInput = prompt("What would you like to do?");
    console.log(userInput);
}

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Because you're declaring a new userInput variable inside the loop, you need to re-assign to the same variable.

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let todos = [];

while (userInput != "q") {
  todos.push(userInput);
  userInput = prompt("What would you like to do?");
}

console.log(todos);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Because you have defined userInput variable inside the while loop again. This makes it locally scoped for the while loop. Just remove let before the variable userInput in while loop.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The problem is that third let. It's creating another userInput, scoped to that inner block, which shadows the outer one.

Try changing it to:

alert("Welcome to your To-Do List!")

let userInput = prompt("What would you like to do?");

let toDo = [];

while (userInput != "q") {
    userInput = prompt("What would you like to do?");
    console.log(userInput);
}

You can read more about let here.

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