Just simple Calculator applictaion by js,css and html.With basic addition, subtraction, multiplication and division functions.
First look at what our calculator looks like:
Based on this, the following is the design idea, why we should write JS like this.
-
start
-
Users use our web-based computer. Open the page, which corresponds to our action of initializing the calculator (
init
function) -> set the event listener of the button at the same time (response to the event of the user clicking the button) -
Next the user clicks the button. As shown in the figure above, our calculator has two buttons, one is numbers, and the other is operation symbols, clearing symbols, etc. Here we need to set two functions to respond. Write an if to determine the type of user button, and then based on the result:
- Is it a number? -> call
handleNumber
- Is it a symbol? -> call
handleSymbol
- Is it a number? -> call
-
handleNumber - Add numbers to `buffer` - update display
-
handleSymbol - Depending on the type of symbol, perform the appropriate action, such as clearing, computing a result, deleting a number, or calling `handleMath`
-
handleMath - If there are pending operations, call `flushOperation` to execute - set `previousOperator` to the current symbol
-
handleMath - If there are pending operations, call `flushOperation` to execute - set `previousOperator` to the current symbol
-
flushOperation - Perform the corresponding math operation according to the `previousOperator`
8.End