Bitpath

Boss lesson · Unit 01

A receipt

Prices, a discount and tax turned into a formatted total

13 exercisesabout 3 min

What you practise

  • Prices, quantities, a discount and tax turned into a formatted total
  • Reading compiler errors and telling them from errors at run time

Try a question from this lesson

Pick one

A shop sells 3 notebooks at 4 each and 2 pens at 1.5 each. Before any code: what should the receipt total be?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Percentages in code

Code has no percent sign for this. Write a percentage as a decimal: 20% is 0.2, 5% is 0.05.

"20% of the price" is price * 0.2. To add tax, add that amount on. To give a discount, take it away.

Name each step. A receipt with subtotal, discount and tax is easy to check by eye.

TypeScript
const price = 50;
const tax = price * 0.2;   // 20% of 50
console.log(price + tax);  // 60

Concept

Two decimals with toFixed

Put .toFixed(2) after a number to get it as text with exactly two decimals, rounded.

It is made for showing money: 0.30000000000000004 becomes 0.30, and 4 becomes 4.00.

A tool like this, used on a value with a dot, is called a method. Its result is a string, so use it last, when you print.

TypeScript
const sum = 0.1 + 0.2;
console.log(sum.toFixed(2));   // 0.30
const price = 4;
console.log(price.toFixed(2)); // 4.00

10 more exercises in the app

  • 1× order the lines
  • 3× type the answer
  • 2× fill the blanks
  • 3× multiple choice
  • 1× true or false

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy