A class based calculator implementation with functions for evaluating mathematical expressions.
currentTotal
: holds the current running total of the calculatorcurrentOperator
: holds the active operator for the calculatorlastOperator
: holds the last operator that was presseddisplayShouldClear
: boolean indicating if the display should be cleared or notonDisplayUpdateHandlers
: an array of functions to handle display updatesonDisplay
: holds the value currently displayed on the calculatorhistory
: an array holding the history of operations performed on the calculator
fireDisplayUpdateHandlers
: invokes all functions inonDisplayUpdateHandlers
arrayonDisplayUpdate
: adds a function to theonDisplayUpdateHandlers
arrayoffDisplayUpdate
: removes a function from theonDisplayUpdateHandlers
arraynumberPressed
: updates the display value of the calculator with the number pressedremoveHangingDecimal
: removes hanging decimal points from the displayevaluate
: evaluates the current mathematical expressionclear
: resets the calculator to its initial state
const calculator = new Calculator();
calculator.onDisplayUpdate((val) => console.log("Calculator display: ", val));
calculator.buttonPressed({ type: "number", value: "2" });
calculator.buttonPressed({ type: "operator", value: "+" });
calculator.buttonPressed({ type: "number", value: "2" });
calculator.evaluate();
// Output:
// Calculator display: 2
// Calculator display: 2
// Calculator display: 4