Workshop › Minesweeper › Step 25 of 26
The best (lowest) time is saved in localStorage. Two traps: "best" means smaller, so the comparison is <; and "no
best yet" must be null, not 0, because a very fast win really can take 0 whole seconds.
```js
// null means "no best time yet"; 0 would be a real (very fast) time.
const saved = localStorage.getItem('mines-best')
let best = saved === null ? null : Number(saved)
const seconds = Math.floor((endTime - startTime) / 1000)
if (best === null || seconds < best) {
best = seconds
localStorage.setItem('mines-best', best)
}
ctx.fillText(state === 'won' ? 'You win! Best: ' + best + 's' : 'Boom! Click to retry', canvas.width / 2, TOP / 2)
```localStorage.getItem gives the saved text, or null if nothing was ever saved. Text becomes a number with
Number(...).let now = 0, write the comment, saved and best.win, write the seconds line and the if block.draw, change 'You win!' to 'You win! Best: ' + best + 's'. Press Run.Skills: Game states
← Stop the clock Build it yourself: wrong flags →