Build a Calculator
A real calculator is harder than it looks. You need to manage:
- State (what was the last operation?)
- Edge cases (dividing by zero, multiple decimals)
- Operator chaining — like a basic pocket calculator, evaluated left-to-right (press 3, +, 4, =, ×, 2, = → 14), not with math precedence
The Spec
Build a Calculator class with:
input(char)— accepts digit, operator (+,-,*,/), decimal, or=getDisplay()— returns the current display stringclear()— reset everything
The Challenge
The tricky part is handling chained operations:
input("3") → display: "3"
input("+") → display: "3"
input("4") → display: "4"
input("=") → display: "7"
input("*") → display: "7"
input("2") → display: "2"
input("=") → display: "14"
Build this step by step. Start simple, then handle edge cases.
⚡ 45 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
basic addition
chained operations
clear resets
multiplication
Run your code to check these.