๐Ÿš€ JavaScript Math Operators
 
JavaScript Math Operators - Interactive Guide

๐Ÿš€ JavaScript Math Operators

Master Arithmetic in Code!

๐ŸŽฏ The Programming Pattern: GET โ†’ CALCULATE โ†’ DISPLAY

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()

+
Addition
Adds two numbers together
5 + 3 = 8
๐Ÿ’ฐ Use for: Total prices, combining amounts
โˆ’
Subtraction
Subtracts one number from another
10 - 4 = 6
๐Ÿ’ต Use for: Change, discounts, remaining amounts
*
Multiplication
Multiplies two numbers
6 * 7 = 42
๐Ÿ• Use for: Repeated amounts, price ร— quantity
/
Division
Divides one number by another
20 / 4 = 5
๐Ÿฐ Use for: Splitting evenly, averages
%
Modulus
Finds the remainder after division
17 % 5 = 2
๐ŸŽฏ Use for: Odd/even checks, leftover amounts

๐Ÿ’ป Live Code Example

Here's how to write a program that calculates pizza cost:

// STEP 1: GET information let pricePerPizza = 12; let quantity = readInt("How many pizzas? "); // STEP 2: CALCULATE the total let total = pricePerPizza * quantity; // STEP 3: DISPLAY the result console.log("Your total is $" + total);

๐Ÿงฎ Interactive Calculator

Click Calculate to see the result!

๐Ÿ“ Practice Problems

  1. ๐ŸŽฎ Gaming Console: You want to buy a $300 console. You save $15/week. Write code to calculate how many weeks you need.
  2. ๐Ÿ” Restaurant Bill: The bill is $68. Split it equally among 4 friends. One has a $10 gift card. How much does each person pay?
  3. ๐Ÿš— Road Trip: Your car gets 28 miles per gallon. Gas costs $3.50/gallon. Calculate cost for a 280-mile trip.
  4. ๐Ÿ“Š Test Average: You scored 85, 92, and 88 on three tests. Calculate your average.

๐Ÿ’ก Pro Tips for Success!

  • โœ… Choose the RIGHT operator: Using + instead of * will give you wrong answers!
  • โœ… Order matters: Use parentheses when you need to add before multiplying
  • โœ… Test your code: Try different numbers to make sure it works correctly
  • โœ… Add comments: Explain what each step does in your code
  • โœ… Variable names: Use descriptive names like totalCost instead of x
Last updated  2026/01/18 08:41:01 PSTHits  96