Exercise: Coffee Shop Receipt
Finish six small functions that work out a coffee shop receipt's subtotal, tax, tip and lines, using numbers, strings, comparisons and conversions, and let the tests check each one.
Novice · JavaScript · Updated
The Daily Grind's order app can take an order, but it can't print a receipt yet. Your job is to write the parts that work out the receipt: the subtotal, the sales tax, the tip, each line of the receipt, whether the customer's loyalty card has earned a free drink, and the quantity they typed. Each part is a small function for you to finish, and tests tell you when each one is right.
Get the exercise code
In the Exercise box beside the lesson, choose Get the starter on GitHub to open its GitHub repository. Click Code, then Download ZIP, and unzip it into your practice folder.
The folder holds:
receipt.js: the six functions you'll finish.print-receipt.js: prints a whole receipt using your functions. Ready to use.receipt.test.js: the tests that check your functions. You don't need to change it.package.json: tells Node that the files share functions withimportandexport. Ready to use.
Open the folder in VS Code, and open a terminal there with Terminal > New Terminal.
What to do
You'll write your own functions in the Functions chapter. Here, each function is already written except for its answer, so it works like a fill-in-the-blank. The names in parentheses are the values the function is handed, and whatever comes after return is its answer. Each one returns a placeholder for now. The comment above it says what it should return, with examples:
/*
* The price of one item times how many were ordered.
*
* subtotal(4.75, 2) -> 9.5
* subtotal(3.25, 3) -> 9.75
*/
export function subtotal(price, quantity) {
// TODO: replace 0 with your calculation.
return 0;
}
export in front lets the tests and print-receipt.js use the function. Module 3's lesson Modules: import and export explains how that works; for now, leave it where it is.
- Name the tax rate. The shop's sales tax is 8%. Give it a name with
constat the top ofreceipt.js, where the comment says. - Finish the money functions:
subtotal, thensalesTaxandtip, which round to the nearest cent. - Finish
receiptLine. It returns one line of the receipt, such as2 x Latte: $9.50, built with a template literal. - Finish
earnedFreeDrinkandquantityFromText: a comparison, and a conversion from typed text to a number.
Check your work
In the terminal, run the tests:
node --test
At first all six tests fail. That's expected: each function you finish turns its test green. When a test fails, its message says which values it used and what it expected, for example The tax on $14.25 is 1.14. Without rounding it comes out as 1.1400000000000001.
To see a whole receipt, run:
node print-receipt.js
Hints
- Work out an example by hand first. What is 8% of $7.35? What is it to the nearest cent?
- Rounding to cents takes three steps: turn dollars into cents, round with
Math.round, then turn cents back into dollars. Numbers and Math shows it. - A percent is a number out of 100. 15% of an amount is the amount times 15, divided by 100.
- A dollar sign before a blank is written
${...}in a template literal; Strings and Template Literals explains why. - Typed text is always a string. Types and Converting Between Them shows how to turn it into a number, and what you get for
'two'.
When you're done
All six tests pass, and node print-receipt.js prints:
The Daily Grind
2 x Latte: $9.50
1 x Blueberry muffin: $3.25
Subtotal: $12.75
Sales tax: $1.02
Tip (20%): $2.55
Total: $16.32
Stamps on your card: 11. Free drink earned: true
Now look back at salesTax: if the tax rate went up to 9%, how many lines of your code would change? If the answer is more than one, look for a 0.08 that should be your const.
So what
Every online checkout does what you built here: multiply, add tax and tip, round to the cent, and turn the numbers into readable text. Small functions, each checked by a test, are how real apps keep money right.