Boss lesson · Unit 01
A receipt
A café bill from prices to a formatted total, and the errors met on the way
What you practise
- Prices, quantities, a discount and tax turned into a formatted total
toFixed- Reading the first errors (
ReferenceError,TypeError, assigning to aconst)
Try a question from this lesson
Pick one
A café bill: 2 coffees at 4.5 each and 1 sandwich at 6. What does this print?
const coffee = 4.5;
const sandwich = 6;
const subtotal = coffee * 2 + sandwich;
console.log(subtotal);Hint
Which operation on line 3 goes first?
Correct answer
15
Why
Multiplication first: 4.5 * 2 is 9, then 9 + 6 is 15. Adding first would give 4.5 * 8, which charges for 8 coffees.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Money needs two decimals
A number prints with only the decimals it needs: 13.2, not 13.20.
total.toFixed(2) gives the number as a string with exactly 2 decimals.
It also hides a surprise: computers store decimals approximately, so a sum can come out a hair off.
const total = 13.2;
console.log(total); // 13.2
console.log(total.toFixed(2)); // 13.20
console.log(0.1 + 0.2);
// 0.30000000000000004Concept
Reading an error
An error message has two parts: the kind of error, then what went wrong.
A ReferenceError is a name JavaScript does not know. A TypeError is a value of the wrong kind for what you asked, such as giving a const a new value.
The program stops at the first error. The lines before it have run. The lines after it never do.
ReferenceError: totl is not defined
TypeError: Assignment to constant
variable.10 more exercises in the app
- 3× type the answer
- 3× multiple choice
- 1× fill the blanks
- 1× match the pairs
- 1× order the lines
- 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.