我遇到了這個程序從You Don't Know JS books書在GitHub上:問題與此程序
const SPENDING_THRESHOLD = 200;
const TAX_RATE = 0.08;
const PHONE_PRICE = 99.99;
const ACCESSORY_PRICE = 9.99;
var bank_balance = 303.91;
var amount = 0;
function calculateTax(amount) {
return amount * TAX_RATE;
}
function formatAmount(amount) {
return "$" + amount.toFixed(2);
}
// keep buying phones while you still have money
while (amount < bank_balance) {
// buy a new phone!
amount = amount + PHONE_PRICE;
// can we afford the accessory?
if (amount < SPENDING_THRESHOLD) {
amount = amount + ACCESSORY_PRICE;
}
}
// don't forget to pay the government, too
amount = amount + calculateTax(amount);
console.log(
"Your purchase: " + formatAmount(amount)
);
// Your purchase: $334.76
// can you actually afford this purchase?
if (amount > bank_balance) {
console.log(
"You can't afford this purchase. :("
);
}
// You can't afford this purchase. :(
我的問題是,如果我改變的bank_balance
到一個更高的值的值也沒關係,但它一直印:You can't afford this purchase.
我已經儘量做到它,所以它不打印:You can't afford this purchase.
我不能使它發揮作用。我開始認爲該計劃是錯誤的,但我認爲只是我。
我知道解決方案很簡單,但我不能看到它,也找不到它。
在這裏和那裏添加一些'console.log'來看看發生了什麼。 – georg
您是否考慮過稅(在選擇購買多少部手機時會被忽略)? – Bergi
如果可能與「在你還有錢的時候繼續購買手機」評論: - ? –