Why localStorage and not just a variable
A plain variable like let high = 0; dies when the page reloads. localStorage is **persistent*: it writes to the browser's disk and comes back next time.
localStorage.setItem('high_score', '5200');
// ... tab closes, reopens, machine restarts ...
localStorage.getItem('high_score'); // still "5200"
The only gotcha: everything stored is a string. Read it back, then parseInt or parseFloat. Also note localStorage is shared across all tabs of the same origin, so writing high_score from one tab is visible in the game tab.
For this game the data is tiny (one number), so localStorage is perfect. For anything sensitive, passwords, API keys, you never store it client-side. That's a web-app concern, not a game one.
Keeping the score fair
A score that grows from time alone could let a player "win" by standing still. The game keeps the numbers honest:
- Coins (
addCoin) give the big, instant reward, the reward for skill. - A small per-second trickle keeps a moving player ahead of a stationary one, without letting idle play win.
- The
highis updated only when the score strictly passes it, and saved immediately, so the record can never be beaten in a single frame by a bug.
if (this.score > this.high) {
this.high = this.score;
this._save();
}
This is the same fairness philosophy from the difficulty caps: the reward should track the player's skill, never the machine's speed or a bug. The tests in the Master tier verify addCoin always adds exactly 100 and that high only grows.