Practice

There is absolutely no substitute for practice in learning programming. No amount of articulate writing on my part is alone going to make you a programmer.

With that in mind, let’s try practicing some of the concepts we learned here in this chapter. I’ll give the “requirements,” and you try it first. Then consult the code listing below to see how I approached it.

  • Write a program to calculate the total price of your phone purchase. You will keep purchasing phones (hint: loop!) until you run out of money in your bank account. You’ll also buy accessories for each phone as long as your purchase amount is below your mental spending threshold.
  • After you’ve calculated your purchase amount, add in the tax, then print out the calculated purchase amount, properly formatted.
  • Finally, check the amount against your bank account balance to see if you can afford it or not.
  • You should set up some constants for the “tax rate,” “phone price,” “accessory price,” and “spending threshold,” as well as a variable for your “bank account balance.””
  • You should define functions for calculating the tax and for formatting the price with a “$” and rounding to two decimal places.
  • Bonus Challenge: Try to incorporate input into this program, perhaps with the prompt(..) covered in “Input” earlier. You may prompt the user for their bank account balance, for example. Have fun and be creative!

OK, go ahead. Try it. Don’t peek at my code listing until you’ve given it a shot yourself!

Note: Because this is a JavaScript book, I’m obviously going to solve the practice exercise in JavaScript. But you can do it in another language for now if you feel more comfortable.

Here’s my JavaScript solution for this exercise:

  1. const SPENDING_THRESHOLD = 200;
  2. const TAX_RATE = 0.08;
  3. const PHONE_PRICE = 99.99;
  4. const ACCESSORY_PRICE = 9.99;
  5. var bank_balance = 303.91;
  6. var amount = 0;
  7. function calculateTax(amount) {
  8. return amount * TAX_RATE;
  9. }
  10. function formatAmount(amount) {
  11. return "$" + amount.toFixed( 2 );
  12. }
  13. // keep buying phones while you still have money
  14. while (amount < bank_balance) {
  15. // buy a new phone!
  16. amount = amount + PHONE_PRICE;
  17. // can we afford the accessory?
  18. if (amount < SPENDING_THRESHOLD) {
  19. amount = amount + ACCESSORY_PRICE;
  20. }
  21. }
  22. // don't forget to pay the government, too
  23. amount = amount + calculateTax( amount );
  24. console.log(
  25. "Your purchase: " + formatAmount( amount )
  26. );
  27. // Your purchase: $334.76
  28. // can you actually afford this purchase?
  29. if (amount > bank_balance) {
  30. console.log(
  31. "You can't afford this purchase. :("
  32. );
  33. }
  34. // You can't afford this purchase. :(

Note: The simplest way to run this JavaScript program is to type it into the developer console of your nearest browser.

How did you do? It wouldn’t hurt to try it again now that you’ve seen my code. And play around with changing some of the constants to see how the program runs with different values.