我想執行一個 do...while 回圈來嘗試這段代碼。
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');
我的目標很簡單,當用戶提示輸入E時,我們退出回圈,滿足whileaction不是E的條件,我們繼續滾動。它不適用于上面的代碼。即使輸入 E,我也陷入無限回圈。
如果我if在回圈內創建新的陳述句條件,我就可以讓它作業,如if action === 'E' {break}. 但后來我不明白為什么 while 陳述句沒有任何價值。
所有這些console.log(action)都是為了除錯目的......
uj5u.com熱心網友回復:
看看這個條件:
while (action !== 'E' || action !== 'e');
它永遠是true:
const action = 'E';
const isFulfilled = action => action !== 'E' || action !== 'e';
console.log(isFulfilled('E'));
console.log(isFulfilled('e'));
console.log(isFulfilled('x'));
你需要的是:
while (action.toLowerCase() !== 'e');
或者,可讀性較差:
while (action !== 'E' && action !== 'e');
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359492.html
標籤:javascript
