Persistence & Application Preferences
25 min read
Load History on Startup
Task
Restore saved history when the application starts.
Open
Open:
js/history.js
Add:
function loadHistory() {
const saved = localStorage.getItem(HISTORY_KEY);
if (!saved) {
return [];
}
try {
const history = JSON.parse(saved);
return Array.isArray(history) ? history : [];
} catch {
return [];
}
}
Then, during application startup, restore the state:
calculator.state.history = loadHistory();
renderHistory(calculator.state.history, historyContainer);
Test
- Perform two or three calculations.
- Refresh the browser.
- Confirm the history is still visible.
- Close and reopen the page if your local development setup allows it.
- Confirm the history remains.
Checkpoint
The calculator now restores saved history when the application starts.