Workshop › Wordle-style Word Guess › Step 24 of 26
Word games live on streaks. Three numbers in one object, { played, won, streak }, are updated and saved in
localStorage every time a game ends. Because the end now does more than set state, it moves into finish(result),
so both endings go through the same code.
```js
let stats = JSON.parse(localStorage.getItem('word-stats') || '{"played":0,"won":0,"streak":0}')
function finish(result) {
state = result
stats.played += 1
if (result === 'won') {
stats.won += 1
stats.streak += 1
} else {
stats.streak = 0
}
localStorage.setItem('word-stats', JSON.stringify(stats))
}
if (current === answer) finish('won')
else if (guesses.length === TRIES) finish('lost')
```localStorage stores only text, so the object is saved as JSON text and read back with JSON.parse. The first time
there is nothing saved, and || falls back to a text of zeros.played and resets the streak.type calls finish instead of setting state itself.let state ..., write the stats line.score function, leave an empty line and write finish.type, replace the two state = ... with finish('won') and finish('lost'). Press Run.Skills: Game states
← Keys that remember Show the statistics →