Math Methods
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Meet the Math Object
JavaScript has a special built-in object called Math that gives you access to many useful mathematical functions. Think of it as a toolbox full of math tools!
To use the Math object, you write Math (with a capital M!), followed by a dot ., and then the name of the method you want to use:
// Rounding a number to the nearest integer
let codemonLevel = 7.8;
let roundedLevel = Math.round(codemonLevel);
console.log("Original level:", codemonLevel);
console.log("Rounded level:", roundedLevel);In the coming exercises, we'll explore several useful Math methods that will help you work with numbers in your Codémon game!
Exercise 2 — Math.round() - Rounding Numbers
Math.round() rounds a number to the nearest integer (whole number). Here's how it works:
- If the decimal part is less than 0.5, it rounds DOWN - If the decimal part is 0.5 or more, it rounds UP
console.log("4.3:", Math.round(4.3));
console.log("4.5:", Math.round(4.5));
console.log("4.7:", Math.round(4.7));Try changing the numbers in the code above to see how Math.round() behaves with different values!
What about negative numbers?
Math.round() works with negative numbers too! Math.round(-4.5) gives -4 (rounds towards positive infinity when exactly halfway).
Exercise 3 — Predicting Math.round()
What will be the value of result after this code runs?
let pyrochuSpeed = 23.5;
let result = Math.round(pyrochuSpeed);Hint : Remember: Math.round() rounds to the nearest integer. When the decimal part is exactly 0.5, it rounds UP.
Exercise 4 — Math.floor() - Always Round Down
Math.floor() is different from Math.round(). It always rounds DOWN to the nearest integer, no matter what the decimal part is!
let damage = 15.9;
let experience = 7.1;
let health = 42.8;
console.log(Math.floor(damage));
console.log(Math.floor(experience));
console.log(Math.floor(health));Notice how Math.floor(15.9) gives 15, not 16! It doesn't care that 15.9 is close to 16 - it always goes down.
The trick is to remember that 'floor' basically forget about the decimal part and just takes the integer part.
Exercise 5 — Choosing the Right Method
Fill in the blanks to use the correct Math method for each situation:
Hint : Remember: Math always starts with a capital M. Use round when you want nearest integer, and floor when you always want to round down.
Exercise 6 — Math.random() - Random Numbers
Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive). This means it can be 0, but it will never be exactly 1!
// Try running this code multiple times!
console.log("Random number:", Math.random());
console.log("Another one:", Math.random());
console.log("And another:", Math.random());Every time you run the code, you'll get different numbers! This is perfect for games where you need randomness.
Scaling random numbers
To get a random number in a different range, multiply Math.random() by your desired maximum. For example, Math.random() * 10 gives a number between 0 and 10.
Exercise 7 — Random Integers with Math.floor()
Math.random() gives decimal numbers, but what if you want random whole numbers? Combine it with Math.floor()!
The pattern is: Math.floor(Math.random() * max)
This gives you a random integer from 0 up to (but not including) max.
// Random integer from 0 to 99
let randomDamage = Math.floor(Math.random() * 100);
console.log("Random damage:", randomDamage);// Random integer from 1 to 6
let diceRoll = Math.floor(Math.random() * 6) + 1;
console.log("Dice roll:", diceRoll);Notice the dice roll example adds + 1 at the end? That's because we want 1-6, not 0-5!
Exercise 8 — Math.max() and Math.min()
Math.max() returns the largest number from the values you give it, while Math.min() returns the smallest number. You can pass as many numbers as you want!
let aquachuAttack = 52;
let pyrochuAttack = 65;
let florachuAttack = 49;
let strongest = Math.max(aquachuAttack, pyrochuAttack, florachuAttack);
console.log(strongest);let aquachuAttack = 52;
let pyrochuAttack = 65;
let florachuAttack = 49;
let weakest = Math.min(aquachuAttack, pyrochuAttack, florachuAttack);
console.log(weakest);Practical uses
These methods are super useful for capping values! For example, Math.min(hp, 100) ensures HP never goes above 100, and Math.max(hp, 0) ensures it never goes below 0.
Exercise 9 — Match the Math Methods
Match each Math method with what it does:
Math.round()Math.floor()Math.random()Math.max()Math.min()Exercise 10 — Codémon Stat Generator
Two wild Codémon appear! Generate their stats and find which one is stronger. Your mission: 1. Generate random HP for Aquachu (between 50 and 100) 2. Generate random HP for Pyrochu (between 50 and 100) 3. Find the Codémon with highest HP 4. Display all three values
// Code here