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

143
Vistas
Forever Do While Loop Javascript

This code keeps looping and looping. I'm using a do..while loop create a simple password checker however.. It keeps looping and looping any help will be greatly appreciated. Thanks

 {
    let password;
   do {
         password = prompt("Enter the code");
      } while (password.toLowerCase() !== "jude" || "joel");
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This:

password.toLowerCase() !== "jude" || "joel"

is not actually what you think it does. It will always evaluate to true, since "joel", when coerced to a boolean, is true (See Truthy and Falsy values).

Demonstration:

const str = "joel";

if (str) {
  console.log("true!");
} else {
  console.log("false!");
}

You have to do the comparison again:

let password;
do {
    password = prompt("Enter the code");
} while (password.toLowerCase() !== "jude" || password.toLowerCase() !== "joel");

Since you're only checking whether the password in lower case is equal or not (pointed out by MikeM), you can also just assign the result of the prompt after being converted to lower case to password. This means we will only need to convert to lowercase once:

let password;
do {
    password = prompt("Enter the code").toLowerCase();
} while (password !== "jude" || password !== "joel");

Or even better, store the valid passwords in an array and check whether it includes the password:

let password;
do {
    password = prompt("Enter the code");
} while (!["jude", "joel"].includes(password.toLowerCase()));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another way that avoids the do while is to use a while with a break; inside.

let password;
while (true) {
    password = prompt("Enter the code");
    if (["jude", "joel"].includes(password.toLowerCase())) {
        break;
    }
}

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