Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda