`You Don`t Know JS!` - question from a book

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:)

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

I believe that if statement is always going to run because it is at the end of your code. So while it executes it will always reach that point when amount is greater than what you entered at prompt() (because you keep adding to amount). You can move the if statement higher and return the string “Sorry you don’t have money” to stop execution.

I think I understood your question, sorry if its a bit unclear.

If you took that straight from the book then I think he meant for it to log to the console every time you eventually run out of money. But if you want it to say “Sorry you don’t have money” earlier then you can move the if statement so it will check that condition while you are buying stuff

1 Like

Well… This is because you have a while loop in your code that keeps purchasing new phones (and possibly accessories) till you run out of balance.

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; 
  }
}
2 Likes

Here look at this… I think this will explain what is happening better.

same code, just moved the console.log()

1 Like

Yes! Thank you so much for all the answers!! i needed to understand more what is happening:) now its all clear! thanks!