Choosing a Word

beginner

Your game always picks the same hardcoded word. That is fine for testing, but not much fun to play. Time to pick a random word.

Sounds simple. But there is a bug hiding in the obvious implementation, and it only shows up sometimes. In this problem, you will learn to test code that involves randomness, and then refactor it so the tricky part becomes easy to test.

  1. Find the bug: write tests for a randomChoice function that has a subtle off-by-one error
  2. Separate the random: refactor so the random part and the array-reading part can be tested independently
  3. Inject the randomness: pass the random function in so tests become deterministic
  4. Wire it into the game: compose randomWord and createGameState into a playable game

Steps