我无法在if/else
函数中获得totalBalance
语句,以便在此代码中正常工作,我不知道为什么。
我尝试将大于或小于切换,并查看它是否解决了结果,但没有任何区别。
var moneyAmount = 0;
var food = 0;
var bills = 0;
var total = 0;
moneyAmount = prompt("how much money do you earn per month?");
amountCheck();
document.write("Your balance is " + "£" + moneyAmount + "<br>");
food = confirm("Do you have any food bills?");
if (food === true) {
food = prompt("How much per week?")
document.write("You spend £" + food + " on food per week <br>");
} else {
alert("Lucky!")
};
totalBalance();
/* total = moneyAmount - food; */
console.log("money amount = " + moneyAmount);
console.log("food = " + food);
console.log("total = " + total);
function totalBalance() {
total = moneyAmount - food;
console.log("total is " + total);
if (total > moneyAmount) {
document.write("Your total amount of money per month is £" + total);
console.log("nay");
} else {
document.write("You need to save more money £" + total);
console.log("yay");
};
}
function amountCheck() {
while (isNaN(moneyAmount)) {
alert("Please enter a numeric value");
moneyAmount = prompt("how much money do you have to spend?");
}
}
&#13;
答案 0 :(得分:2)
if (total > moneyAmount)
永远不会评估为真,除非食物是负值。不确定你到底想要什么,但根据上下文,简单地将if语句更改为if (total > 0)
对我来说更有意义。