This project is part of The Odin Project: Calculator, where I build a basic calculator that performs simple math operations.
It includes the four primary operations: addition, subtraction, multiplication, and division.
Live Demo: https://jambalong.com/calculator/
- Perform basic arithmetic operations: add, subtract, multiply, and divide.
- Display that updates with user input and the result of operations.
- A "Clear" button to reset the calculator and start fresh.
- Ability to chain calculations using previous results.
- Rounding of answers with long decimals to prevent display overflow.
- Error handling for invalid operations (e.g., dividing by 0).
- Add support for decimal numbers with a decimal point button.
- Style improvements: distinguish operations from the keypad with different colors.
- A "Backspace" button to undo input mistakes.
-
Calculator Functions
- Create functions to handle basic arithmetic:
add
,subtract
,multiply
, anddivide
. - Write a function
operate
that takes an operator and two numbers, then calls the appropriate arithmetic function.
- Create functions to handle basic arithmetic:
-
HTML Calculator Layout
- Build a basic calculator interface with buttons for digits, operators, an "Equals" button, and a "Clear" button.
- Add a display that shows user input and the result.
-
JavaScript Logic
- Write the logic to capture user input and display it on the calculator screen.
- Store input values and operators, then perform calculations using the
operate
function. - Update the display with the result after each operation.
-
Handle Edge Cases and Bugs
- Prevent multiple calculations from being evaluated at once.
- Round answers with long decimals.
- Ensure pressing "=" without full input doesn't break the calculator.
- Display an error message if the user tries to divide by 0.
-
Extra Credit
- Add decimal support, ensuring no more than one decimal point per number.
- Improve CSS styling for a more polished look.
- Add a "Backspace" button for corrections.
- Enable keyboard support for efficient input.
-
Clone the repository to your local machine
git clone https://github.com/jambalong/calculator.git
-
Navigate into the project directory
cd calculator/
-
Open the
index.html
file in your browser to use the calculator. -
For development:
- Open the project in your favorite code editor.
- Use live server or similar tools to preview the project in your browser.