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

270
Views
Keep showing prompt, until correct value is given in javascript

I am trying to show the prompt everytime, the user gives a null or incorrect input. Only upon, giving the correct entry, will the prompt go and an alert will be shown. This is my code:

var pass=prompt("Please enter the password to view your recovery code.");
while(true){
    if(pass=="abc"){
        break;
    }
    else
       var pass=prompt("Please enter the password to view your recovery code."); 
}

alert("correct");

Now, even if I give the correct value (abc), the prompt still remains and the alert never shows. Why?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It can be simpler than that. Add the prompt inside the loop, and break if the password is a match.

while (true) {

  const pass = prompt('Please enter the password to view your recovery code.');
  
  if (pass === 'abc') {
    console.log('Correct!');
    break;
  }

}

about 4 years ago · Juan Pablo Isaza Report

0

The error was because you had re-define the variable pass with the line var pass=prompt(...)

You should also consider using let instead of var

Also, i've updated the condition do make something a little bit cleaner

let pass = prompt("Please enter the password to view your recovery code.");
while(pass !== "abc"){
       pass = prompt("Please enter the password to view your recovery code."); 
}

alert("correct");

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!