Arithmetic Operations

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Basic Arithmetic Operators

When working with numbers in JavaScript, you'll use arithmetic operators to perform calculations: - Addition + - Subtraction - - Multiplication * - Division /

Try running this code:

let addition = 10 + 5; let subtraction = 20 - 8; let multiplication = 7 * 3; let division = 100 / 4; console.log(addition, subtraction, multiplication, division);

Exercise 2 — Predicting Results

What will be the value of result after this code runs?

let pyrochuAttack = 45; let bonusAttack = 15; let result = pyrochuAttack + bonusAttack;
Options
• 60
• 30
• 4515
• 675

Hint : The + operator adds the two numbers together. Calculate 45 + 15.

Exercise 3 — Calculating Damage

Complete the code to calculate the remaining HP after Florachu takes damage in battle.

Hint : When a Codémon takes damage, its HP decreases. Which operator reduces a number?

Exercise 4 — Special Operators: Modulo and Power

JavaScript has two more powerful arithmetic operators that are incredibly useful:

Modulo operator % The modulo operator gives you the remainder after division. For example, 10 % 3 equals 1 because when you divide 10 by 3, you get 3 with a remainder of 1.

let result = 10 % 3; console.log(result);

Exponentiation operator ** The exponentiation operator raises a number to a power. For example, 2 ** 3 means 2 to the power of 3, which is 2 × 2 × 2 = 8.

let result = 2 ** 3; console.log(result);

Common Use Cases

Modulo is often used to: - Check if a number is even (n % 2 === 0) or odd (n % 2 === 1) - Create repeating patterns or cycles - Find remainders when distributing items Exponentiation is often used to: - Calculate exponential growth - Compute areas and volumes (side ** 2 for area) - Create difficulty curves in games

Exercise 5 — Understanding Modulo

What is the result of 23 % 5?

Options
• 3
• 4
• 4.6
• 5

Hint : The modulo operator % returns the remainder. When you divide 23 by 5, how many times does 5 go into 23, and what's left over?

Exercise 6 — Calculating Squared Values

Complete the code to calculate the area of a square training field for your Codémon (area = side × side).

Hint : To square a number (multiply it by itself), you can use the exponentiation operator with a power of 2.

Exercise 7 — Order of Operations

When you combine multiple operators in one expression, JavaScript follows the order of operations (also known as PEMDAS):

1. Parentheses: () - Evaluated first 2. Exponentiation: ** - Evaluated second 3. Multiplication and Division: * / % - Evaluated left to right 4. Addition and Subtraction: + - - Evaluated last, left to right

Let's see how this affects calculations in a Codémon battle:

let result1 = 10 + 5 * 2; let result2 = (10 + 5) * 2; console.log(result1, result2);

When in Doubt, Use Parentheses!

Even if parentheses aren't strictly necessary, using them can make your code clearer and prevent mistakes. 10 + 5 * 2 works, but 10 + (5 * 2) is easier to understand.

Exercise 8 — Order of Operations Challenge

What will be the value of result?

let result = 2 + 3 ** 2 * 2; console.log(result);
Options
• 20
• 50
• 100
• 28

Hint : Remember the order: First exponentiation (**), then multiplication (*), then addition (+). Calculate step by step.

Exercise 9 — Match Operators to Use Cases

Match each arithmetic operator with its most appropriate use case in a Codémon game:

Items to match
• +
• -
• *
• /
• %
• **
Definitions
• Adding HP when healing a Codémon
• Reducing HP when taking damage
• Calculating critical hit (double damage)
• Splitting rewards among team members
• Checking if level is even
• Calculating exponential XP requirements

Exercise 10 — Codémon Battle Calculator

Time to put your knowledge together! Create a battle damage calculator. Your task: 1. Calculate the total damage by adding attackPower and bonus 2. Calculate how many hits needed to defeat the enemy using enemyHP and total damage 3. Calculate the remaining HP after complete hits using modulo Print all results using console.log().

Starter code
let attackPower = 50; let bonus = 20; let enemyHP = 180; // Your code here
Back to the course