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

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

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