Operations
JavaScript · Unit 1: First steps
- Free
- JavaScript
- 10 exercises
+, -, * and / work like on a calculator.
Watch out for /: it gives the exact result, with decimals if there are any.
console.log(3 * 4);
console.log(7 / 2);Prints
12 3.5
% gives what's left over from a division, and ** raises to a power.
7 % 2 is 1 because the 2 fits three times into the 7 and 1 is left over.
console.log(7 % 2);
console.log(2 ** 3);Prints
1 8
Math.floor() cuts the decimals downwards and Math.round() rounds to the nearest whole number.
console.log(Math.floor(7 / 2));
console.log(Math.round(7 / 2));Prints
3 4
Math.max() gives you the highest of the values you pass it and Math.min() the lowest.
console.log(Math.max(8, 3, 10));
console.log(Math.min(8, 3, 10));Prints
10 3
First ** is worked out, then *, / and %, and + and - at the end.
Whatever goes inside parentheses is worked out before anything else.
console.log(10 - 2 * 3);
console.log((10 - 2) * 3);Prints
4 24
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
const pizza = 7; const people = 2; console.log(pizza / people);2. Put the lines in order
Order the lines to work out a bill with a 20 % tip.
3. Multiple choice
What is
10 % 3worth?4. Complete the code
Fill in so it prints 8 and then 1.
console.log(2 ___ 3); console.log(7 ___ 2);5. Predict the output
What does this code print?
console.log(Math.floor(4.7)); console.log(Math.round(4.7));6. Complete the code
Fill in to print the highest grade.
const emma = 78; const jack = 92; console.log(Math.___(emma, jack));7. Find the bug
This program should say how many hours and minutes 135 minutes are. Which line has the error?
const mins = 135; const hours = mins / 60; const rest = mins % 60; console.log(hours, "h", rest, "min");8. Predict the output
Multiplication goes before addition. What does it print?
console.log(2 + 3 * 4);9. Find the bug
This program should print the average of 4 and 6. Which line has the error?
const a = 4; const b = 6; const average = a + b / 2; console.log(average);10. Predict the output
17 cookies are shared out among 5 kids. What does it print?
const cookies = 17; const kids = 5; const each = cookies / kids; console.log(Math.floor(each)); console.log(cookies % kids);
It opens in your browser. It is free and you do not need an account to start.