Used useRef hook to store mutable calculator data with objects as data structure.
- It does not pass the fCC test suite.
Will use the useState to store calculator data.
- Create a copy of the state's array. This will be used as an argument passed into the necessary functions to process.
- Or, create a callback within the setState function. [reference] (https://www.technetexperts.com/react-usestate-array-not-updating-a-common-problem-solved/)
- Arithmetic expression/equation (e.g.,
'3 + 5 x 6 - 2 / 4'), consists of:- numbers: positive and negative whole and/or decimal numbers,
- operators: arithmetic operators and equals (
x, /, +, -), - equality sign:
=, - result value: a single value equal simplification of terms by arithmetic operation.
- Array for the arithmetic expression.
- String elements, the numbers and operators.
- Variable holds the result value.
- expression state for updating expression
- display prop - contains the value of the expression state, for rendering display:
- displays the inputted expression and the complete equation,
- displays the latest component (number, operator, result) within the
#displayelement.
- Capture mouse-input.
- Identify the applicable task:
- Forman expression: numbers & operators.
- Complete / simplify the expression, calculate the result: equals operator.
- Clear / reset the calculator data.
- Expression:
- Store input into the array.
- Inclusion / non-inclusion of the previous calculation result to start the expression.
- access the result in the expression array, expect valuse like 'Infinity' and 'NaN'.
- restart the expression array for a new expression.
- Validate for the correct sequence of expression.
- By condition(s), integrate a negative sign to numbers.
- Calculation:
- Necessary correction to the expression before calculation.
- Calculate result using MDAS rule.
- Manage precision of result's decimal figures not less than four digits.
- Clear: resets the expression state to its initial state.