Project: Calculator Logic
Write the logic as pure functions first, then connect it to buttons. Separating logic from the page makes it easy to test.
Example
const ops = { "+": (a, b) => a + b, "-": (a, b) => a - b, "*": (a, b) => a * b, "/": (a, b) => (b === 0 ? NaN : a / b) };
const calc = (a, op, b) => ops[op](a, b);
console.log(calc(6, "*", 7), calc(1, "/", 0), calc(9, "-", 4));Exercise
Look up an operation.
ops[]
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.