Javascript · Numbers
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Which operator for remainder?
Which operator gives you the remainder of a division?
Options
• ```%```
• ```/```
• ```**```
• ```//```
Hint : Think about which arithmetic operator is specifically designed to give you the remainder, not the quotient.
Exercise 2 — Triple a value
Complete the code to triple Aquachu's power using a compound operator:
let aquachuPower = 20; aquachuPower ____ ____; console.log(aquachuPower);
Options: *= += **= 3 60 20
Hint : Compound operators let you modify a variable based on its current value in a shorter way.
Exercise 3 — Predicting Math.floor()
What is the result of
Math.floor(18.9)
?
Options
• ```18```
• ```19```
• ```18.9```
• ```20```
Hint : Remember the difference between ```Math.floor()``` and ```Math.round()``` - one always goes in a specific direction.
Exercise 4 — Match the operation
Match each code with its result:
Items to match
• 10 + 5
• 10 - 5
• 10 * 5
• 10 / 5
Definitions
• 15
• 5
• 50
• 2
Exercise 5 — Identifying NaN
Which of these operations will produce
NaN
?
Options
• ```0 / 0```
• ```10 / 0```
• ```10 / 2```
• ```10 * 0```
Hint : ```NaN``` appears when a math operation produces an undefined result. Which operation is mathematically impossible?
Exercise 6 — Finding the maximum
Complete the code to find the highest score among three Codémons:
let score1 = 85; let score2 = 92; let score3 = 78; let highest = ____.____(score1, score2, score3); console.log(highest);
Options: Math math max min Max maximum
Hint : The Math object has methods to compare values. Think about what the opposite of minimum is.
Exercise 7 — Understanding exponentiation
What is the result of
2 ** 4
?
Options
• ```16```
• ```8```
• ```6```
• ```24```
Hint : Exponentiation means multiplying a number by itself multiple times. Count how many times carefully.
Exercise 8 — Rounding with Math.round()
What does
Math.round(7.4)
return?
Options
• ```7```
• ```8```
• ```7.4```
• ```7.5```
Hint : ```Math.round()``` looks at the decimal part to decide which way to round. What's the rule for values below 0.5?
Exercise 9 — Using the modulo operator
Complete the code to find the remainder when 17 is divided by 5:
let total = 17; let divisor = 5; let remainder = total ____ divisor; console.log(remainder);
Options: % / ** * // ÷
Hint : You need an operator that gives the remainder, not the result of the division itself.
Exercise 10 — Compound assignment result
What will be the value of
x
after this code?
let x = 100; x /= 4; console.log(x);
Options
• ```25```
• ```104```
• ```96```
• ```400```
Hint : Compound assignment operators modify the variable in place. Think about what operation is being performed.
Exercise 11 — Match Math methods
Match each Math method with its purpose:
Items to match
• Math.random()
• Math.min()
• Math.floor()
• Math.round()
Definitions
• Generate random decimal between 0 and 1
• Find the smallest value
• Round down to nearest integer
• Round to nearest integer
Exercise 12 — Understanding Infinity
What is the result of
100 / 0
?
Options
• ```Infinity```
• ```NaN```
• ```0```
• ```100```
Hint : Division by zero doesn't cause an error in JavaScript. It produces one of the special numeric values you've learned.
Exercise 13 — Order of operations
What number completes this code to make
result
equal to
20
?
let result = 5 + 3 * ____; console.log(result);
Options: 5 3 6 15 4 2
Hint : Order of operations matters! Multiplication is calculated before addition. Work backwards from the expected result.
Exercise 14 — Sequential operations
What is the final value of
energy
?
let energy = 50; energy += 20; energy *= 2; energy -= 30; console.log(energy);
Options
• ```110```
• ```80```
• ```90```
• ```70```
Hint : Compound operators are executed in sequence, from top to bottom. Track the value after each operation.
Exercise 15 — Understanding Math.random() range
Which statement about
Math.random()
is TRUE?
Options
• It can be 0 but never 1
• It can be 1 but never 0
• It can be both 0 and 1
• It can never be 0 or 1
Hint : Pay attention to the terms 'inclusive' and 'exclusive' when describing ranges. They tell you about the boundaries.
Exercise 16 — Random integer from 0 to 9
Complete the code to generate a random integer from 0 to 9:
let randomDigit = Math.floor(Math.random() * ____); console.log(randomDigit);
Options: 9 10 8 11
Hint : Count how many different values are possible in the desired range. That's your multiplier.
Exercise 17 — Pick the right Math tool
Match each piece of code with the job it does in a Codémon game:
Items to match
• Math.min(hp, 100)
• Math.max(hp, 0)
• Math.floor(Math.random() * 5)
• Math.round(87.6)
Definitions
• Stop HP from ever going above 100
• Stop HP from ever going below 0
• Pick a random whole number from 0 to 4
• Show a decimal HP as the closest whole number
Exercise 18 — The type of NaN
What does this code display?
let result = 0 / 0; console.log(typeof result);
Options
• ```"number"```
• ```"NaN"```
• ```"undefined"```
• ```"string"```
Hint : Two of these options are not even types that typeof can return. For the rest, think about what kind of operation produced this value in the first place.
Exercise 19 — Checking for NaN
Complete the code to check if
result
is
NaN
:
let result = 0 / 0; let isInvalid = ____.____(result); console.log(isInvalid);
Options: Number isNaN Math NaN isFinite checkNaN
Hint : There's a special function to check for ```NaN```. It's part of the Number object and uses dot notation.
Exercise 20 — Which expression gives 30?
Which of these expressions gives
30
?
Options
• ```(5 + 5) * 3```
• ```5 + 5 * 3```
• ```5 * 5 + 3```
• ```5 + 3 * 5```
Hint : The same three numbers appear everywhere, so only the order in which JavaScript reaches them changes. Work each option out step by step.
Exercise 21 — Finding minimum value
What does
Math.min(45, 23, 67, 12, 89)
return?
Options
• ```12```
• ```23```
• ```45```
• ```89```
Hint : This Math method compares all the values. Think about what 'min' might stand for.
Exercise 22 — Squaring with compound operator
Complete the code to square the value of
damage
(multiply it by itself):
let damage = 5; damage ____ ____; console.log(damage);
Options: **= *= 2 12 ^=
Hint : Squaring involves multiplication with a specific power. There's a compound operator for exponentiation.
Exercise 23 — Match special number values
Match each operation with the special value it produces:
Items to match
• Math.sqrt(-16)
• 50 / 0
• -100 / 0
• 0 / 0
Definitions
• NaN
• Infinity
• -Infinity
• NaN
Exercise 24 — Combining Math methods
What is the result of this code?
let a = 12.7; let b = 12.3; let result = Math.max(Math.round(a), Math.round(b)); console.log(result);
Options
• ```13```
• ```12```
• ```12.7```
• ```12.3```
Hint : Functions are evaluated from the inside out. What happens to each value first, before comparing them?
Exercise 25 — Checking for finite numbers
Complete the code to check if
score
is a finite number (not
Infinity
and not
NaN
):
let score = 100 / 2; let isValid = ____.____(score); console.log(isValid);
Options: Number isFinite Math finite isInfinity checkFinite
Hint : There's a Number method that checks if a value is neither infinite nor invalid. Look for the opposite of 'infinite'.
Back to the course