Students will use arithmetic operators to write JavaScript programs that solve problems with 80% accuracy.
By the end of class, you'll write programs that calculate costs, split bills, and animate graphics!
๐ Warm-Up: The Mystery Restaurant Bill
Four friends went to dinner. The bill was $68.
They want to split it equally, BUT one friend has a $10 gift card.
How much does each person actually pay?
Think: What calculations do you need? In what order?
๐ฏ The Programming Pattern
1๏ธโฃ
GET
Collect information from the user using readInt() or readFloat()
2๏ธโฃ
CALCULATE
Use operators (+, โ, *, /, %) to solve the problem
3๏ธโฃ
DISPLAY
Show results using console.log()
Every program you write will follow this pattern!
๐ข JavaScript Math Operators
+
Addition
Adds two numbers
5 + 3 = 8
โ
Subtraction
Subtracts numbers
10 - 4 = 6
*
Multiplication
Multiplies numbers
6 * 7 = 42
/
Division
Divides numbers
20 / 4 = 5
๐ The Mysterious Modulus Operator
What is % (Modulus)?
It finds the REMAINDER after division
17 % 5 = 2
Because 17 รท 5 = 3 remainder 2
๐ก Use for: Checking odd/even, finding leftovers, time conversion
๐ป Live Demo: Pizza Program
Problem: Calculate total cost for pizzas at $12 each
// STEP 1: GET informationlet pricePerPizza = 12;let quantity = readInt("How many pizzas? ");// STEP 2: CALCULATE the totallet total = pricePerPizza * quantity;// STEP 3: DISPLAY the resultconsole.log("Your total is $" + total);
โ ๏ธ Common Mistake: Wrong Operator!
โ WRONG
12 + 5 = 17
Using + gives you the wrong answer!
5 pizzas should NOT cost $17!
โ CORRECT
12 * 5 = 60
Using * multiplies correctly!
5 pizzas at $12 each = $60
Choose the RIGHT operator or your program breaks!
๐ฏ How to Choose the Right Operator
Combining amounts? โ Use + (total savings, combined scores)
Finding what's left? โ Use โ (change, discounts)
Repeated items? โ Use * (price ร quantity)
Splitting equally? โ Use / (split bills, averages)
Finding remainder? โ Use % (odd/even, leftovers)
๐ Quia Interactive Practice
Before we code in CodeHS, let's see these operators in action!
Watch how changing operators changes results completely
๐ฐ CodeHS 2.5.3: Bookstore
Your Task:
Write a program for a bookstore that:
Asks how many books the customer wants
Calculates the total cost (books are $15 each)
Displays the total
Example: 3 books should cost $45
๐ฆ CodeHS 2.5.4: Flying Pigeon
Animation = Math Repeated Quickly!
To move something across the screen, you change its position using math:
// Move right: ADD to x positionpigeon.x = pigeon.x + 3;// Move up: SUBTRACT from y positionpigeon.y = pigeon.y - 1;
Every 0.05 seconds, the computer recalculates position!
๐ Debug Challenge: Find the Bugs!
// Movie ticket program - has 3 bugs!let ticketPrice = 10let numTickets = readInt("How many tickets? ");let popcorn = 6;let total = ticketPrice + numTickets + popcorn;console.log("Your total is: " total);let change = 50 / total;console.log("Your change from $50 is: $" + change);
Can you find all 3 bugs? Think about operators and syntax!
๐งฉ Practice Problems
Problem 1: Gaming Console ๐ฎ
Console costs $300. You save $15/week. Calculate weeks needed.
Problem 2: Road Trip ๐
Car gets 28 mpg. Gas costs $3.50/gallon. Calculate cost for 280-mile trip.
๐ก Pro Tips for Success!
โ Choose the RIGHT operator - Using + instead of * breaks everything!
โ Order matters - Use ( ) to control calculations
โ Test your code - Try the example numbers first
โ Add comments - Explain what each step does
โ Variable names matter - Use totalCost not x
๐ญ Reflection
When would using the WRONG operator completely break your program?
Give a specific example from today.
Think-Write-Share with a partner
๐ Homework Choice Board
Pick 2 of 4 activities:
Bill Splitter Pro ๐ต - Split bills with tip options (15%, 18%, 20%)