Quería ejecutar un bucle do...while para probar este código.
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'); Mi objetivo es simplemente, cuando el usuario ingresa E cuando se le solicita, salimos del ciclo y cumplimos con la condición de que mientras la action no es E, seguimos rodando. No funciona con el código anterior. Estoy atascado con un bucle infinito incluso cuando ingresé E.
Puedo hacer que funcione si creo una nueva condición de instrucción if dentro del ciclo, como if action === 'E' {break} . Pero entonces no entiendo por qué la declaración while no vale nada.
todos los console.log(action) son para fines de depuración ...
Mira esta condición:
while (action !== 'E' || action !== 'e'); Siempre será true :
const action = 'E'; const isFulfilled = action => action !== 'E' || action !== 'e'; console.log(isFulfilled('E')); console.log(isFulfilled('e')); console.log(isFulfilled('x'));Lo que necesitas es:
while (action.toLowerCase() !== 'e');O, menos legible:
while (action !== 'E' && action !== 'e');