A project to demonstrate the use of JavaScript and DOM manipulation, by recreating the Apple calculator app on a web page.
Live Project Screenshot- To learn how to manipulate the DOM on user interaction using JavaScript EventListeners
- For number and operator button clicks to trigger changes in the display, perform the math operation and output the correct result on the display
- Use HTML/SCSS to reproduce the appearance of the following screenshot:
- HTML
- CSS/SCSS
- JavaScript
- Git
HTML/SCSS
- Create a calculator to be rendered to the html page
- it should have number keys from 0 to 9
- It should have operator keys (+, -, /, *, =)
- It should have a display rendering the current calculation in a box at the top
- It should also have a "." key
- You should choose a picture of a calculator from the Internet and reproduce it in HTML/SCSS
JavaScript
- Should render the current calculation in a box at the top (calculator display)
- It should handle decimals
- It doesn't need to support orders of operation
- It should not use eval() or Function() constructor
Prior to coding the JavaScript, I formed a plan for the logic of the calculator to help structure my subsequent working code.
- Each button should have an EventListener to do something when clicked
- Number Buttons
- Replace content of display if display 0 or previous button was an operator
- Concatenate to display if display is a number or a .
- . Button
- Concatenate to current displayed number or 0
- AC Button
- Reset display to 0 and clear stored values
- Operator Buttons
- Evaluate any previous operations in memory involving current displayed value and any stored values, storing the result (all operator buttons should action an "=" first)
- Store value currently on display if no previous operator
- Store the operator that has just been clicked
- Allow next number to be entered for current operation
- +/- Button
- Negate current displayed value and output to display
- % Button
- Divide current displayed number by 100 before evaluating any operations
- Different buttons types were grouped by class
- I iterated through the NodeList for the Operator and Number classes separately to apply EventListeners that would run different logic depending on if the button is an Operator or Number
- The operands (previously displayed and current), and operator for the current operation are stored in global variables
- I modularised the code, separating into generic DOM interactions (dom-utils.js), generic math functions (calc.js), and page-specific code (script.js).
I encountered the following issues during implementation and implemented a fix for each case.
- Allow decimal place input
- Auto-add 0 to first place when making a new decimal number
- Prevent more than one decimal point being entered
- Long number overflow (over 10 characters) that doesn't fit the display => implemented the toExponential() method
- Entering number after "3RR0R" message does not reset display
- Different calculator style options
- Additional buttons and operators for different styles