Application State Management
Relevant source files
The following files were used as context for generating this wiki page:
- calculator.html
- README.md
- LICENSE
- .gitignore
- index.html (Alias for primary application file)
The Application State Management system within the Calculator project is a client-side architecture designed to maintain the integrity of mathematical operations, UI synchronization, and user preferences. It utilizes a centralized set of global variables within the JavaScript runtime to track the lifecycle of a calculation, including operand input, operator selection, and result computation.
The system ensures that the Document Object Model (DOM) accurately reflects the internal logic state, providing immediate visual feedback for user interactions. Beyond active calculations, the state management also encompasses a historical record of operations and persistence for user interface themes (light/dark modes).
Core State Variables
The application's logic is driven by five primary state variables that define the current context of the calculator at any given moment.
| Variable | Type | Initial Value | Description |
|---|---|---|---|
current | String | '0' | The value currently being typed or the result shown in the main display. |
previous | String | '' | The stored value from the previous step of a multi-part calculation. |
operator | String/Null | null | The mathematical operator (+, −, ×, ÷) selected for the current operation. |
shouldReset | Boolean | false | A flag indicating if the next digit input should overwrite the current display. |
history | Array | [] | An array of objects storing previous calculation expressions and results. |
Sources: calculator.html:324-329
Calculation Lifecycle and Data Flow
The state transitions through various phases based on user input. When a user inputs a digit, the inputDigit(d) function updates the current state. If an operator is selected via handleOp(op), the current value is migrated to previous, and the operator state is set.
The following diagram illustrates the flow of data when a user performs a standard addition operation:
Sources: calculator.html:349-399
Arithmetic Processing
The compute(a, b, op) function serves as the pure logic engine, taking state values as arguments and returning a calculated result. To maintain precision and avoid floating-point artifacts, the system utilizes a round(n) helper that limits results to 12 significant digits.
function compute(a, b, op) {
switch (op) {
case '+': return round(a + b);
case '−': return round(a - b);
case '×': return round(a * b);
case '÷': return b === 0 ? 'Error' : round(a / b);
default: return b;
}
}
function round(n) { return parseFloat(n.toPrecision(12)); }
Sources: calculator.html:381-392
History State Persistence
The application maintains a session-based history state. Every time a user triggers handleEquals(), a new record is generated and prepended to the history array. The system imposes a hard limit on this state to prevent memory bloat.
- Maximum Records: 20 entries.
- Structure: Each entry is an object:
{ expr: string, result: number }. - Interaction: Clicking a history item restores that specific
resultinto the activecurrentstate variable, effectively performing a state "rollback" to a previous value.
Sources: calculator.html:416-444
UI State Synchronization
The system employs several helper functions to ensure the DOM (the visual layer) remains in sync with the internal JavaScript state.
updateDisplay(val, animate): Synchronizes thecurrentvariable with the main number display, including formatting for locale and scientific notation if the number exceeds $10^{12}$.updateExpr(val): Synchronizes the sub-display showing the mathematical expression currently being built (e.g., "10 + ").renderHistory(): Completely clears and rebuilds the history list in the DOM whenever thehistoryarray is modified.
Sources: calculator.html:336-347, calculator.html:422-440
Theme State Management
The visual theme (Light vs. Dark) is managed through a data-attribute on the root HTML element. Unlike the mathematical state, the theme state is toggled via an Immediately Invoked Function Expression (IIFE) that listens for system preferences or user overrides.
Sources: calculator.html:470-484
The state management system in the Calculator project effectively separates user input handling, mathematical computation, and visual rendering. By using a combination of global primitives for calculation and a structured array for history, the application provides a robust and predictable user experience.