Day 5 — The first Challenge.

So, today I began solving the first challenge on the “You Don’t Know JS: Up & Going”, on Chapter 1.

I will repeat the problem here as a reminder:

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!

…and… here is my solution…

… I had a problem on testing it because its just a plain JS file. The solution is to open the developer tools in Firefox (for example)on a new empty tab by right click -> inspect element -> Console. Then paste your code inside the console prompt in the down left corner and click enter.

If you try to paste your code on the same window you will get an error like this:

In that case you need to close this tab, open a new one, and paste your code in the console again.

The Official Solution

This was the solution of the book author:


After completing all the challenges, contributing on Git is your next big objective. If you don’t already have an account, register on and create a new repository. After that upload your code, manually in my case.

Next stop: Chapter 2