Back

Activity 1.4: Arithmetic Operations

divider

Activity 1.4

Arithmetic Operations

Key Concepts

Arithmetic Operators

Order of Operations

Numbers in JavaScript

Numbers can be negative, and long numbers can use underscores to stay readable.

console.log(-1953);
console.log(1_000_000);

Arithmetic Operators

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus (remainder)

Modulus

The % operator gives you the remainder left over after division.

console.log(10 * 10.2);
console.log(450 / 5);
console.log(10 % 3);

Order of Operations

JavaScript follows the same order of operations you use in math class — multiplication and division happen before addition and subtraction, unless parentheses say otherwise.

Today's Objectives

  • Using arithmetic operators on variables
  • Using the modulus operator to find a remainder
  • Controlling order of operations with parentheses

Key Terms

Operator
A symbol that performs an operation on values, like + or *.
Modulus
The remainder left over after division, using %.
Order of Operations
The rules that decide which operations happen first in an expression.

'F' → Fullscreen

divider

Build

Create a new JavaScript file named 1-4-arithmetic.js.


Task 1: Addition and Subtraction

  • Declare two variables and add them together.
  • Declare a variable and subtract from it.
1-4-arithmetic.js
let amount1 = 1000;
let amount2 = 500.96;
console.log("Adding 1000 and 500.96:");
console.log(amount1 + amount2);
let apples = 3;
console.log("Apples after eating one:");
console.log(apples - 1);

Task 2: Multiplication and Division

  • Use multiplication to convert an age in years to months.
  • Use division to split a number of pizza slices evenly among people.
1-4-arithmetic.js
let amount1 = 1000;
let amount2 = 500.96;
console.log("Adding 1000 and 500.96:");
console.log(amount1 + amount2);
let apples = 3;
console.log("Apples after eating one:");
console.log(apples - 1);
let age = 30;
console.log("Age in months:");
console.log(age * 12);
let pizzaSlices = 8;
let people = 2;
console.log("Pizza slices per person:");
console.log(pizzaSlices / people);

Task 3: Modulus

  • Use the modulus operator to find how many dollars are left over after splitting a total evenly among a group.
1-4-arithmetic.js
let amount1 = 1000;
let amount2 = 500.96;
console.log("Adding 1000 and 500.96:");
console.log(amount1 + amount2);
let apples = 3;
console.log("Apples after eating one:");
console.log(apples - 1);
let age = 30;
console.log("Age in months:");
console.log(age * 12);
let pizzaSlices = 8;
let people = 2;
console.log("Pizza slices per person:");
console.log(pizzaSlices / people);
let totalDollars = 100;
let numPeople = 6;
console.log("Leftover dollars after splitting evenly:");
console.log(totalDollars % numPeople);

Task 4: Order of Operations

  • Write an expression that relies on order of operations, and another that uses parentheses to change the result.
1-4-arithmetic.js
let amount1 = 1000;
let amount2 = 500.96;
console.log("Adding 1000 and 500.96:");
console.log(amount1 + amount2);
let apples = 3;
console.log("Apples after eating one:");
console.log(apples - 1);
let age = 30;
console.log("Age in months:");
console.log(age * 12);
let pizzaSlices = 8;
let people = 2;
console.log("Pizza slices per person:");
console.log(pizzaSlices / people);
let totalDollars = 100;
let numPeople = 6;
console.log("Leftover dollars after splitting evenly:");
console.log(totalDollars % numPeople);
let x = 4;
let y = 10;
let z = 12;
console.log("x * y + z equals:");
console.log(x * y + z);
console.log("x * (y + z) equals:");
console.log(x * (y + z));
divider

Checkpoint

Verify your program works correctly.

Example Output
Adding 1000 and 500.96:
1500.96
Apples after eating one:
2
Age in months:
360
Pizza slices per person:
4
Leftover dollars after splitting evenly:
4
x * y + z equals:
52
x * (y + z) equals:
88
divider

Reflection

Answer the following questions before submitting your work.

  1. What does the modulus operator (%) actually calculate?
  2. What is the result of 4 * 10 + 12? What about4 * (10 + 12)? Why are they different?
  3. Why is it useful to store values in variables before doing arithmetic on them, instead of typing the raw numbers directly?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete