Hello everyone!
As i am now reading a book called “You dont know JS”, i am having trouble understanding an example in it.
It is a simple program to calculate the total price of a phone purchase.
Here it is:
const SPENDING_TRESHOLD = 200;
const TAX_RATE = 0.08;
const PHONE_PRICE = 99.99;
const ACCESSORY_PRICE = 9.99;
var bank_balance = prompt ("How much money you have:");
var amount = 0;
function calculateTax(amount) {
return amount * TAX_RATE;
}
function formatAmount(amount) {
return "$" + amount.toFixed(2);
}
// keep buying phones while you still have the money
while (amount < bank_balance){
amount = amount + PHONE_PRICE; // buy a new phone
if ( amount < SPENDING_TRESHOLD){ //can we afford the accessory?
amount = amount + ACCESSORY_PRICE;
}
}
amount = amount + calculateTax(amount);
console.log("Your purchase:" + formatAmount(amount));
// can you afford this ??
if (amount > bank_balance) {
console.log("Sorry, you dont have enough money...");
}
my problem is that if i input a bigger amount to bank_balance still returns “sorry you dont have enough money”… (or in the original version it was a variable and not a prompt. so like this: var = bank_balance = 303.91 --> and if i set this to 400 or even higher, the “Your purchase:xxx” is getting also higher…)
maybe it wasnt the clearest explanation… but maybe i ve missed something so obvious, that it is easy to correct anyway… i dont think that the book has a wrong answer:)
thank you sooo much:)