📚 › JavaScript: make pages do things
14. Mini project: a simple calculator
Put the ideas together: a function takes two numbers and an operator, and returns the result. Then a small switch chooses the calculation.
Small programs like this are where most beginners finally feel they can code.
Show example
function calc(a, b, op) {
if (op === "+") return a + b;
return a * b;
}
console.log(calc(3, 4, "+"));
✍️ Your turn
Write calc(a, b, op) that adds when op is + and multiplies otherwise. Print calc(6, 7, "*").