typescript · level 1 · unit 3 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
TypeScript is a calculator that never gets tired.
Each maths symbol is an . + adds, - takes away, * multiplies and / divides. Keyboards have no times sign, so we use the star instead.
A is a number too, so 7 / 2 gives 3.5, and 3.5 fits in a number box. Whole numbers and decimals share one type.
Like at school, * and / happen before + and -. So 2 + 3 * 4 is 14. Wrap a part in brackets to make it happen first.
console.log(7 / 2);
console.log(2 + 3 * 4);
console.log((2 + 3) * 4);
let price: number = 1.5;
console.log(price * 4); Never put a comma inside a number, like 1,000. Write 1000.
guess first
Ten sweets, four friends. What does each get?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let sweets: number = 10;
let friends: number = 4;
console.log(sweets / friends);
your turn
Your turn. Plan a pizza party.
Type how many guests are coming and how many slices each one eats, as numbers. Then press run to see how many slices you need.
fix it
Splitting the bill between two. Fix the maths!
- The answer comes out as 11, not 7. Which operator happens first?
/happens before+, so only the drinks get halved. You want the whole bill halved.- Change line 3 to (pizza + drinks) / 2, then press run.
nailed it
you turned code into a calculator. :D
+,-,*and/are maths operators.*and/happen before+and-.- Decimals and whole numbers are both
number.
new words in your collection
- operator
- decimal
This lesson isn’t saved. A free account remembers every lesson you finish.