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

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

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 Denunciar

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 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