Workshop › Typing Rain › Step 11 of 33

Lock on to a word

What we are doing

Which word are you typing? You never say; the game works it out. The first letter you type locks on to a word starting with it. If several match, the lowest one is the most urgent.

```js
let target // the word being typed, or null
let typed // how many letters of the target are typed

  target = null
  typed = 0

function type(key) {
  if (!target) {
    // Lock on to the lowest word starting with this letter: it is the most urgent.
    const options = words.filter((w) => w.text[0] === key).sort((a, b) => b.y - a.y)
    if (options.length === 0) return
    target = options[0]
    typed = 0
  }
}
```

What it means

Your turn

  1. Under let words write let target and let typed.
  2. In reset, under words = [], write target = null and typed = 0.
  3. Under reset, after an empty line, write type.

Skills: Game states, Arrays and objects

All steps

  1. A sky for the words
  2. The ground
  3. A word on the canvas
  4. A function that draws
  5. A list of words
  6. Make them fall
  7. The game loop
  8. A new word from the list
  9. Let it rain
  10. Gone past the ground
  11. Lock on to a word
  12. Listen to the keyboard
  13. Letter by letter
  14. Half a word in yellow
  15. Let go of a lost word
  16. Lives
  17. Hearts
  18. Score
  19. Out of lives
  20. Freeze the game
  21. The game over panel
  22. Play again
  23. Levels
  24. Faster and faster
  25. Best score
  26. Accuracy
  27. Words per minute
  28. Your speed at the end
  29. Where the keys go
  30. Draw the keyboard
  31. Light up the next key
  32. Tap the keys
  33. Build it yourself: change your mind