Skip to content

Simple Calculator

Easy~20 min

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. 1

    Define the function signature

    Create a function calculate(a, op, b) where a and b are numbers and op is a string like "+", "-", "*", "/". The signature tells the caller exactly what to provide.

    Code
    function calculate(a, op, b) {
      // body comes next
    }

    Expected result

    The function exists and can be called, but returns nothing yet.

  2. 2

    Select the operation

    Inside the function, branch on op and perform the matching arithmetic. Returning inside each branch keeps the logic flat and readable.

    Code
      if (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. 3

    Guard division by zero

    Before dividing, check whether b === 0 and return a safe value or message. This prevents Infinity or a runtime error from leaking to the caller.

    Code
      if (op === "/") {
        if (b === 0) return "Cannot divide by zero";
        return a / b;
      }

    Expected result

    calculate(5, "/", 0) returns the error message instead of Infinity.

  4. 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.

    Code
    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 zero

    Expected result

    Five lines of output: 9, 7, 42, 4, and the division-by-zero message.

Try it yourself

What 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

Related Tools

  • Math Evaluator

    Compare your calculator with a full expression evaluator.

Related Cheatsheets

Related Reference

Related Errors

Related Glossary

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.

← Back to topic