📚 › 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.
उदाहरण देखें
function calc(a, b, op) {
if (op === "+") return a + b;
return a * b;
}
console.log(calc(3, 4, "+"));
✍️ अब आपकी बारी
Write calc(a, b, op) that adds when op is + and multiplies otherwise. Print calc(6, 7, "*").