# 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 November 14, 2026

By Gregor Dzierzon

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

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-javascript-coffee-shop-receipt](https://github.com/ask-codey/exercise-javascript-coffee-shop-receipt), also linked from the **Exercise** box beside the lesson. Clone it into your practice folder. In a terminal, in that folder:

```bash
git clone https://github.com/ask-codey/exercise-javascript-coffee-shop-receipt.git
```

Git makes a folder, `exercise-javascript-coffee-shop-receipt`, with the exercise code inside.

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 with `import` and `export`. 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:

```javascript
/*
 * 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.

1. **Name the tax rate.** The shop's sales tax is 8%. Give it a name with `const` at the top of `receipt.js`, where the comment says.
2. **Finish the money functions:** `subtotal`, then `salesTax` and `tip`, which round to the nearest cent.
3. **Finish `receiptLine`.** It returns one line of the receipt, such as `2 x Latte: $9.50`, built with a template literal.
4. **Finish `earnedFreeDrink` and `quantityFromText`:** a comparison, and a conversion from typed text to a number.

## Check your work

In the terminal, run the tests:

```bash
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:

```bash
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:

```text
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`.

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. You don't need Git for this: you upload your files in the browser, as the lesson *Put Your Code on GitHub* (Module 1) shows screen by screen.

1. On GitHub, click **+**, then **New repository**. (No account yet? Sign up at github.com: it's free.) Name it (for example `exercise-javascript-coffee-shop-receipt`), choose **Public**, turn **Add README** on so the upload button appears, and click **Create repository**. Your folder's own README replaces GitHub's when you upload it.
2. Choose **Add file**, then **Upload files**. In your exercise folder, select everything (Ctrl+A, or Cmd+A on a Mac) and drag it onto the upload area. Drag what's inside the folder, not the folder itself, so `package.json` lands at the top of your repo.
3. Under **Commit changes**, type a short note, such as *My solution*, and choose **Commit changes**.
4. Paste your repo's address, such as `https://github.com/your-name/exercise-javascript-coffee-shop-receipt`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Upload the changed files again (each one replaces the file with the same name), commit, and submit again: your latest submission is the one that counts.

## 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.

## Key terms

- [const](https://www.ask-codey.com/glossary/const-javascript)
- [Template Literal](https://www.ask-codey.com/glossary/template-literal)
- [Math Object](https://www.ask-codey.com/glossary/math-object)
- [Strict Equality (===)](https://www.ask-codey.com/glossary/strict-equality)
- [NaN](https://www.ask-codey.com/glossary/nan)

## Related

- [Variables with let and const](https://www.ask-codey.com/lessons/javascript-variables-let-and-const)
- [Numbers and Math](https://www.ask-codey.com/lessons/javascript-numbers-and-math)
- [Strings and Template Literals](https://www.ask-codey.com/lessons/javascript-strings-and-template-literals)
- [Booleans and Comparisons](https://www.ask-codey.com/lessons/javascript-booleans-and-comparisons)
- [Types and Converting Between Them](https://www.ask-codey.com/lessons/javascript-types-and-converting)

Source: https://www.ask-codey.com/lessons/exercise-javascript-coffee-shop-receipt
