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

281
Vistas
JS : Why does my do ... while loop is not exited?

I wanted to execute a do...while loop to try this code.

var savings, chequing, action;
savings = 1000;
chequing = 1000;
function checkAccounts() {
    alert("You have $" + savings + " in your savings and $" + chequing + " in your chequin account");
}

function withdrawal() {
    let amount = +prompt("How much would you like to withdraw?");
    //return the withdrawal value and store it in a variable called spending money
    return amount;
}

alert("Hello, welcome to the bank! What would you like to do?");
do {
    action = prompt("You can decide to see check your accounts (C), withdraw some money (W), or exit (E). Please choose one of those 3 actions");
    console.log(action);
    if (action === 'C' || action === 'c') {
        checkAccounts();
    } else if (action === 'W' || action === 'w') {
        let account = prompt("From which account would you like to withdraw some money? (S)avings or (C)hecking account");
        if (account === 'S' || account === 's') {
            let spendingMoney = withdrawal();
            savings -= spendingMoney;
        } else if (account === 'C' || account === 'c') {
            let spendingMoney = withdrawal();
            chequing -= spendingMoney;
        }
        alert("After this operation, here are the details of your account :");
        checkAccounts();
        console.log(action);
    }
    console.log(action);
} while (action !== 'E' || action !== 'e');

My goal is simply, when the user enters E when prompted, we exit the loop and meet the condition that while action is not E, we keep rolling. It does not work with the code above. I'm stuck with an infinite loop even when entered E.

I'm able to make it work if I create new if statement condition inside the loop, as if action === 'E' {break}. But then I don't understand why the while statement is not worth anything.

all the console.log(action) are for debugging purpose...

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Look at this condition:

while (action !== 'E' || action !== 'e');

It'll always be true:

const action = 'E';
const isFulfilled = action => action !== 'E' || action !== 'e';

console.log(isFulfilled('E'));
console.log(isFulfilled('e'));
console.log(isFulfilled('x'));

What you need is:

while (action.toLowerCase() !== 'e');

Or, less readably:

while (action !== 'E' && action !== 'e');
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