Workshop › 15 Puzzle › Step 26 of 27
Here fewer moves is better. We keep the best result in localStorage so it survives a reload, and show it at the
top right while playing.
```js
let best = Number(localStorage.getItem('fifteen-best')) || 0
state = 'solved'
if (best === 0 || moves < best) {
best = moves
localStorage.setItem('fifteen-best', best)
}
ctx.fillText(state === 'solved' ? 'Solved! Click to shuffle' : best ? 'Best ' + best : '', canvas.width - LEFT, 36)
```localStorage.getItem reads the saved text (or null); Number(...) || 0 makes it a number, 0 if there is none.best === 0) or fewer moves than the record.let slide ... write the best line.move, under state = 'solved', write the if block.draw, change : '' in the last line to : best ? 'Best ' + best : ''. Press Run.Skills: Game states
← Play again Build it yourself: a timer →