Reset and Clear Functionality
Relevant source files
The following files were used as context for generating this wiki page:
- calculator.html
- README.md
- LICENSE
- .github/workflows/static.yml
- index.html (Identical to calculator.html in this project context)
The Reset and Clear Functionality in the Calculator application provides users with mechanisms to revert the application state, wipe current calculations, and manage historical data. This system ensures that the internal state variables—current, previous, and operator—are properly synchronized with the Document Object Model (DOM) elements to prevent calculation errors and maintain a clean user interface.
This functionality is divided into two primary scopes: the Global State Reset (AC), which clears the immediate calculation registers, and the History Clear, which purges the persistent log of previous calculations stored during the session.
Application State Reset
The core of the reset logic resides in the handleClear function. This function is responsible for zeroing out the internal calculation engine. It targets the four primary state variables that track the user's progress through a mathematical expression.
State Variable Management
When a clear action is triggered, the following state transitions occur:
| Variable | Reset Value | Description |
|---|---|---|
current | '0' | The value currently being typed or displayed. |
previous | '' | The operand stored before an operator was selected. |
operator | null | The arithmetic operation currently pending. |
shouldReset | false | A boolean flag indicating if the next digit input should overwrite the display. |
Sources: calculator.html:362-365
UI Synchronization
The reset process concludes by updating the UI components to reflect the clean state. The updateDisplay('0') call ensures the main output reads "0", and updateExpr('') clears the secondary expression line above the main result.
Sources: calculator.html:436-439
The following flowchart illustrates the logic flow when the "AC" button or the Escape key is pressed:
The diagram shows how the system handles both UI interactions and keyboard shortcuts to achieve a synchronized reset.
Sources: calculator.html:436-439, calculator.html:470, calculator.html:482
History Clear Functionality
The history system maintains an array of objects representing previous calculations. The clear functionality for this module is decoupled from the main calculator reset to allow users to clear their workspace without losing their calculation log, or vice versa.
History List Purging
The clear history action targets the history array and the historyList DOM container. It performs the following operations:
- Array Truncation: Re-initializes the
historyarray to an empty state[]. - DOM Re-rendering: Calls
renderHistory(), which removes all existing.history-itemelements from the view. - Empty State Toggle: Displays the
#historyEmptymessage ("No calculations yet") once the list is empty.
Sources: calculator.html:459-462, calculator.html:446-447
Sequence of History Erasure
The interaction between the user and the history management system is depicted below:
This sequence ensures that the persistent record of session calculations is fully removed from both memory and the interface.
Sources: calculator.html:446-457, calculator.html:459-462
Input Overwrite (Implicit Reset)
The system implements a "soft reset" or "overwrite" logic using the shouldReset flag. This is not a manual clear operation but an automated state management feature that occurs after a calculation is completed or an operator is selected.
- Trigger: Setting
shouldReset = truehappens afterhandleOporhandleEquals. - Action: When
inputDigitis called whileshouldResetis true, the current value is replaced by the new digit rather than appended, effectively resetting the input buffer for the next part of the calculation.
Sources: calculator.html:388-390, calculator.html:405, calculator.html:432
function inputDigit(d) {
if (shouldReset) {
current = d === '0' ? '0' : d;
shouldReset = false;
}
// ... rest of logic
}
Sources: calculator.html:388-393
Summary of Triggers
Reset and clear actions can be invoked through multiple interfaces:
| Action | UI Element | Keyboard Shortcut | Function |
|---|---|---|---|
| All Clear | .btn-fn[data-action="clear"] | Escape | handleClear() |
| Clear History | #clearHistory | N/A | Anonymous Arrow Function |
| Input Reset | N/A (Automatic) | Any Digit (Post-equals) | inputDigit() logic |
Sources: calculator.html:267, calculator.html:314, calculator.html:470, calculator.html:482