Skip to content

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");
    if (action === 'C' || action === 'c') {
    } 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 :");
} 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…


Look at this condition:

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

It’ll always be true:

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


What you need is:

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

Or, less readably:

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