Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!