javascript · 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
JavaScript is a calculator that never gets tired.
Each maths symbol is an . + adds, - takes away, * multiplies (a star, not an x) and / divides. Numbers can have a decimal point too, like 2.5.
Dividing can leave bits, so 7 / 2 gives 3.5. Sharing 7 slices of pizza between 2 friends? Each gets 3 whole slices, and 7 % 2 gives the : the 1 slice left over.
Like on paper, * and / happen before + and -. Brackets beat them all, so wrap the part you want worked out first.
console.log(7 / 2);
console.log(7 % 2);
console.log(2 + 3 * 4);
console.log((2 + 3) * 4); Here % has nothing to do with percentages: it finds what is left over.
guess first
Ten sweets, four friends. What is left over?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let sweets = 10;
let friends = 4;
console.log(sweets % friends);
your turn
Your turn. Share out the stickers.
Type how many stickers you have and how many friends to share them with. Then press run to see how many are left over for you.
fix it
Each friend should pay 7, not 11. Fix it!
- Two friends split the whole bill. Which part is JavaScript working out first?
/happens before+, so only the drinks get shared. Add them up first.- Wrap pizza + drinks in round brackets on line 3, then press run.
nailed it
you built a tireless calculator. :D
+,-,*and/are operators for maths.%gives the remainder, the bit left over.*and/go first, unless brackets say otherwise.
new words in your collection
- operator
- remainder
This lesson isn’t saved. A free account remembers every lesson you finish.