Workshop › Wordle-style Word Guess › Step 17 of 26

Winning and losing

What we are doing

A guess that matches the answer wins; six that do not, lose. A state remembers which. Once the game is over, keys do nothing, except Enter, which starts a new word.

```js
let state // 'playing', 'won' or 'lost'

  state = 'playing'

function type(key) {
  if (state !== 'playing') {
    if (key === 'Enter') reset()
    return
  }

  else if (key === 'Enter' && current.length === 5) {
    guesses.push({ word: current, marks: score(current, answer) })
    if (current === answer) state = 'won'
    else if (guesses.length === TRIES) state = 'lost'
    current = ''
```

What it means

Your turn

  1. Under let current, write let state ...; in reset, under current = '', write state = 'playing'.
  2. At the top of type, write the if (state !== 'playing') block.
  3. In the Enter block, remove && guesses.length < TRIES and write the two state lines under guesses.push(...).

Skills: Game states

All steps

  1. The canvas and the pen
  2. Paint the background
  3. A function that draws
  4. A row of five tiles
  5. Six rows
  6. Draw again and again
  7. The typed letters
  8. Type with the keyboard
  9. Show the letters
  10. Backspace
  11. Leave shortcuts alone
  12. A secret word
  13. Right letter, right place
  14. Right letter, wrong place
  15. Enter sends the guess
  16. Colour the guesses
  17. Winning and losing
  18. A message line
  19. Where the keys go
  20. Draw the keyboard
  21. Tap a key
  22. Enter and Delete keys
  23. Keys that remember
  24. Keep statistics
  25. Show the statistics
  26. Build it yourself: only real words