Calculator
Hard · Reducers
Build a simple calculator. Render <div className="display">0</div> and one <button> for each key: "0" to "9", "+", "-", "*", "/", "=" and "C".
- Digits build the current number. A leading "0" is replaced ("0", "5" shows "5").
- An operator remembers the current number and operator; the next digit starts a new number.
- If an operation is already waiting and a new number was typed, pressing another operator first calculates the result and shows it (left to right: 2 + 3 * shows "5", then 4 = shows "20").
- Pressing two operators in a row just replaces the operator (9 - + 3 = gives "12").
- "=" shows the result. A digit after "=" starts a new number.
- Dividing by zero shows "Error".
- "C" clears everything back to "0".
Show results with String(number), e.g. 7 / 2 = shows "3.5".
Next: Mini Spreadsheet