# Calculator > Build a simple calculator that supports basic arithmetic operations, decimal numbers, and negative values, while handling edge cases in user input. - Difficulty: advanced - Tags: useState - Estimated time: 60 minutes - Tests: 18 - Requires subscription: yes ## Overview Build a simple calculator that supports basic arithmetic operations, decimal numbers, and negative values, while handling edge cases in user input. ## Requirements - Display the current expression as the user types. - Evaluate mathematical expressions correctly when the equals button is pressed, respecting operator precedence (`*` and `/` before `+` and `-`). - Support the four basic operations: addition, subtraction, multiplication, and division. - Allow decimal numbers and negative numbers. - Prevent invalid inputs or sequences: - The expression cannot start with `+`, `*`, or `/`. - The expression can start with `-` to represent a negative number. - Disallow consecutive operators except when using `-` to indicate a negative number. - Avoid multiple dots within the same number. - Treat a lone `.` at the start or right after an operator as `0.` (e.g., `.5` → `0.5`). - Prevent evaluation when the last element is an operator or when the expression is empty. - Handle negative numbers properly after any operator. - Allow correcting a pending unary `-` after an operator by pressing another operator (e.g., `5+-` then `+` → `5+`). - Prevent more than two consecutive `-` signs. - Replace leading zeros when typing new numbers (e.g., `03` → `3`). - Allow chaining of operations and continuous usage after evaluating a result: - If a result is displayed and the user presses an operator, continue from that result. - If a result is displayed and the user presses a number, start a new expression. - Clear the display and reset the calculator when pressing `C`. ## Notes - The display should show either the full expression or the result, depending on context. - Expressions must update immediately as buttons are pressed. - Consecutive operators must be handled gracefully (e.g., replacing the previous one if needed), including a pending unary `-` (e.g., `5+-` then `+` → `5+`). - Evaluation must respect operator precedence (`*` and `/` before `+` and `-`). It can be implemented without `eval` using a small expression parser. - Negative numbers after operators (e.g., `5*-2`) must be valid. - A lone dot is normalized to `0.` both at the start and after an operator. - The calculator should handle decimal precision correctly (round results to two decimals if needed). - Avoid any syntax errors or invalid evaluations when using `eval` or equivalent. - Clearing should reset both the expression and the result. ## Tests 01. basic sum operation 02. more complex sum operation 03. does not allow '+' as the first value 04. does not allow '*' as the first value 05. does not allow '/' as the first value 06. allows '-' as the first value (negative numbers) 07. allow negative numbers after any operator 08. prevents more than two consecutive '-' and allows negative numbers correctly 09. handle leading zeros correctly 10. if there is a result and a number or operator is pressed 11. prevent multiple dots in the same number 12. prevent consecutive operators 13. replaces a pending negative sign when another operator is pressed 14. treats a lone dot as 0. 15. concatenate numbers/dots if the last element is a number 16. prevent evaluating if the last element is an operator 17. prevent evaluating if is empty 18. clear