๐Ÿ”ข

JavaScript Numbers

Master arithmetic operations and the Math object

โญ Beginner Friendly

What are Numbers?

Numbers in JavaScript represent numeric values - from ingredient quantities to cooking temperatures, timer durations to recipe ratings. JavaScript has only one number type that handles both integers and decimals.

๐Ÿ’ก Real-World Example:

Scaling a recipe from 4 servings to 6 servings requires multiplying all ingredient quantities by 1.5!

Common Number Operations

1. Basic Arithmetic

const eggs = 6;
const servings = 4;
console.log(eggs + 2);  // 8 (addition)
console.log(eggs - 2);  // 4 (subtraction)
console.log(eggs * 2);  // 12 (multiplication)
console.log(eggs / 2);  // 3 (division)

2. Math Object Methods

const temperature = 175.7;
console.log(Math.round(temperature)); // 176
console.log(Math.floor(temperature)); // 175
console.log(Math.ceil(temperature));  // 176
console.log(Math.max(100, 200, 150)); // 200
console.log(Math.min(100, 200, 150)); // 100

3. Parsing Strings to Numbers

const input = "25";
console.log(parseInt(input));    // 25
console.log(parseFloat("3.14")); // 3.14
console.log(Number("42"));       // 42

4. Fixed Decimal Places

const price = 12.3456;
console.log(price.toFixed(2)); // "12.35"

๐Ÿ”„ Incrementing

let count = 5;
count++;        // 6
count += 3;     // 9
count -= 2;     // 7

๐Ÿ“Š Percentage Calculation

let total = 200;
let percent = 15;
let result = (total * percent) / 100;
// 30

๐ŸŽฏ Precision

let num = 5.6789;
parseFloat(num.toFixed(2)); // 5.68
parseInt("42px");           // 42

๐Ÿ”ข Type Checking

typeof 42;              // "number"
Number.isInteger(42);   // true
Number.isNaN(NaN);      // true

Learn More

๐Ÿš€ Try It Yourself

Output:

๐Ÿ“

Game 1: Recipe Scaling Calculator

Scale recipes up or down! Convert ingredient amounts when you need more or fewer servings.

๐Ÿ“‹ Original Recipe (Serves 4)

๐Ÿฅ› Flour 2 cups
๐Ÿฅš Eggs 3 eggs
๐Ÿงˆ Butter 0.5 cups
๐Ÿฌ Sugar 1.5 cups

๐ŸŽฏ Scaled Recipe

๐Ÿ’ก Math Used: Enter servings and click Calculate!
โฑ๏ธ

Game 2: Cooking Timer Challenge

Practice time conversions! Convert between seconds, minutes, and hours for perfect cooking timing.

โฐ Set Timer

๐ŸŽฎ Quick Challenges

โš–๏ธ

Challenge 1: Recipe Scaling Calculator

๐Ÿ“‹ Your Mission:

Create a function that scales recipe ingredients based on the number of servings:

  1. Calculate the scaling factor (newServings / originalServings)
  2. Multiply the ingredient quantity by the scaling factor
  3. Round the result to 2 decimal places

Example:

scaleIngredient(2, 4, 6) โ†’ 3

(2 cups for 4 servings, scaled to 6 servings = 3 cups)

๐Ÿ’ก Hints:

  • Divide newServings by originalServings to get the multiplier
  • Use toFixed(2) for rounding
  • Convert back to number with parseFloat()

Test Results:

โฒ๏ธ

Challenge 2: Cooking Timer Challenge

๐Ÿ“‹ Your Mission:

You have multiple dishes cooking. Calculate when to start each dish so they all finish at the same time:

  1. Find the maximum cooking time among all dishes
  2. Calculate start time offset for each dish
  3. Return the offset in minutes

Example:

calculateStartTime([30, 45, 20]) โ†’ [0, 15, 10]

(45 is max, so first starts immediately, second waits 15 min, third waits 10 min)

๐Ÿ’ก Hints:

  • Use Math.max(...times) to find longest time
  • Offset = maxTime - dishTime
  • Use .map() to calculate all offsets

Test Results: