有3個值,
previousBalanceTotal
= 這是銀行賬戶的總余額previousWithdrawTotal
= 我以前提取過這么多總價值newWithdrawAmount
= 我現在要提取的金額,
我現在需要設定newWithdrawTotal
。
我想做的是,如果我newWithdrawAmount
的比我的大previousBalanceTotal
,我想顯示一個警報,并保持newWithdrawTotal
之前的WithdrawTotal,否則添加 newWithdrawAmount
到previousWithdrawTotal
并保留它newWithdrawTotal
。
newWithdrawAmount
我嘗試了以下代碼,但它在大于時不起作用previousBalanceTotal
。它回傳一個函式并且不顯示警報。你能幫我解決這個問題嗎?
const newWithdrawTotal = (newWithdrawAmount < previousBalanceTotal) ? (newWithdrawAmount previousWithdrawTotal) : function () {
console.log(previousWithdrawTotal);
alert('you can not withdraw more than your balance');
return previousWithdrawTotal;
};
console.log(newWithdrawTotal);
uj5u.com熱心網友回復:
你沒有呼叫函式,你只是回傳它。使用 IIFE 立即呼叫匿名函式。
const newWithdrawTotal = (newWithdrawAmount < previousBalanceTotal) ?
(newWithdrawAmount previousWithdrawTotal) : (function() {
console.log(previousWithdrawTotal);
alert('you can not withdraw more than your balance');
return previousWithdrawTotal;
})();
console.log(newWithdrawTotal);
您實際上并不需要該功能,您可以改用逗號運算子。
const newWithdrawTotal = (newWithdrawAmount < previousBalanceTotal) ? (newWithdrawAmount previousWithdrawTotal) : (
console.log(previousWithdrawTotal),
alert('you can not withdraw more than your balance'),
previousWithdrawTotal
);
console.log(newWithdrawTotal);
uj5u.com熱心網友回復:
試試這個代碼塊
const newWithdrawAmount = 13000;
const previousBalanceTotal = 12000;
const previousWithdrawTotal = 300;
var newWithdrawTotal;
function calc() {
if(newWithdrawAmount < previousBalanceTotal) {
return newWithdrawTotal = newWithdrawAmount previousWithdrawTotal
} else {
console.log(previousWithdrawTotal);
alert('you can not withdraw more than your balance');
return newWithdrawTotal = previousWithdrawTotal;
}
}
calc()
console.log(newWithdrawTotal);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/506846.html
標籤:javascript 功能 返回 条件运算符
上一篇:如何修復此錯誤:TypeError:unsupportedoperandtype(s)for*:'float'and'function'inpython