Simple Calculator
What you'll build
A `calculate(a, op, b)` function that supports +, -, *, / and returns the numeric result, with a guard against division by zero.
Goals
- Define a function with multiple parameters
- Use a conditional or match to select the operation
- Return a value from a function
- Handle an edge case (division by zero)
Prerequisites
- Variables and data types (numbers)
- if/else or switch/match statements
- Function definition concept
Steps
- 1
Define the function signature
Create a function
calculate(a, op, b)whereaandbare numbers andopis a string like "+", "-", "*", "/". The signature tells the caller exactly what to provide.Codefunction calculate(a, op, b) { // body comes next }Expected result
The function exists and can be called, but returns nothing yet.
- 2
Select the operation
Inside the function, branch on
opand perform the matching arithmetic. Returning inside each branch keeps the logic flat and readable.Codeif (op === "+") return a + b; if (op === "-") return a - b; if (op === "*") return a * b; if (op === "/") return a / b;Expected result
Calling calculate(4, "+", 5) returns 9; calculate(10, "*", 3) returns 30.
- 3
Guard division by zero
Before dividing, check whether
b === 0and return a safe value or message. This prevents Infinity or a runtime error from leaking to the caller.Codeif (op === "/") { if (b === 0) return "Cannot divide by zero"; return a / b; }Expected result
calculate(5, "/", 0) returns the error message instead of Infinity.
Hint
Order matters: check the zero case before performing the division.
- 4
Test all operators
Call the function once for each operator and print the results. Testing every branch is how you confirm the function is complete.
Codeconsole.log(calculate(4, "+", 5)); // 9 console.log(calculate(10, "-", 3)); // 7 console.log(calculate(6, "*", 7)); // 42 console.log(calculate(8, "/", 2)); // 4 console.log(calculate(5, "/", 0)); // Cannot divide by zeroExpected result
Five lines of output: 9, 7, 42, 4, and the division-by-zero message.
Try it yourself
function calculate(a, op, b) {
// TODO: implement +, -, *, / with a zero-division guard
}
// Test calls
console.log(calculate(4, "+", 5));
console.log(calculate(8, "/", 0));function calculate(a, op, b) {
if (op === "+") return a + b;
if (op === "-") return a - b;
if (op === "*") return a * b;
if (op === "/") {
if (b === 0) return "Cannot divide by zero";
return a / b;
}
return "Unknown operator";
}
console.log(calculate(4, "+", 5)); // 9
console.log(calculate(10, "-", 3)); // 7
console.log(calculate(6, "*", 7)); // 42
console.log(calculate(8, "/", 2)); // 4
console.log(calculate(5, "/", 0)); // Cannot divide by zeroWhat you learned
You built a function that accepts parameters, branches on input, returns a value, and handles an edge case — the four ingredients of most real functions.
Related Resources
Related Lessons
- Functions
How to define and call functions with parameters.
- Conditions
Branching with if statements, used to select the operator.
Related Practice
- Functions Concept
Solidify the idea of parameters and return values.
- Data Types MCQ
Numbers and strings are the types this calculator works with.
Related Tools
- Math Evaluator
Compare your calculator with a full expression evaluator.
Related Cheatsheets
- JavaScript Cheatsheet
Function syntax and arithmetic operators reference.
Related Reference
- JavaScript Math
Arithmetic operations and number handling reference.
Related Errors
- Type Error
Operating on incompatible types.
- Name Error
Referencing undefined names.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Type Error
Operating on incompatible types.
- Name Error
Referencing undefined names.