Master arithmetic operations and the Math object
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!
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)
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
const input = "25";
console.log(parseInt(input)); // 25
console.log(parseFloat("3.14")); // 3.14
console.log(Number("42")); // 42
const price = 12.3456;
console.log(price.toFixed(2)); // "12.35"
let count = 5;
count++; // 6
count += 3; // 9
count -= 2; // 7
let total = 200;
let percent = 15;
let result = (total * percent) / 100;
// 30
let num = 5.6789;
parseFloat(num.toFixed(2)); // 5.68
parseInt("42px"); // 42
typeof 42; // "number"
Number.isInteger(42); // true
Number.isNaN(NaN); // true
Scale recipes up or down! Convert ingredient amounts when you need more or fewer servings.
Practice time conversions! Convert between seconds, minutes, and hours for perfect cooking timing.
Create a function that scales recipe ingredients based on the number of servings:
Example:
scaleIngredient(2, 4, 6) โ 3
(2 cups for 4 servings, scaled to 6 servings = 3 cups)
๐ก Hints:
toFixed(2) for
rounding
parseFloat()
You have multiple dishes cooking. Calculate when to start each dish so they all finish at the same time:
Example:
calculateStartTime([30, 45, 20]) โ [0, 15, 10]
(45 is max, so first starts immediately, second waits 15 min, third waits 10 min)
๐ก Hints:
Math.max(...times)
to find longest time
.map() to
calculate all offsets