Validate a Guess
Before processing a guess, we should check that it is valid. What makes a guess invalid? List the cases before you code. Each case is a test.
validateGuess(letter, guessedLetters) checks whether a guess is acceptable and returns an object:
- Valid:
{ valid: true } - Invalid:
{ valid: false, reason: "..." }
Rules
- Input must be a single alphabetic character. Digits, symbols, empty strings, and multi-character strings are invalid with reason
"not a letter" - A letter that has already been guessed is invalid with reason
"already guessed" - The duplicate check is case-insensitive. If
"a"was guessed,"A"is a duplicate
Write tests first
Before you touch solution.js, open tests.js and write assertions for every rule above. When a test fails, you want to know which case broke. Both assert.equal and assert.deepEqual accept an optional third argument, a message that shows up on failure:
assert.deepEqual(result, expected, "digit should be rejected")
Once your tests are in place (they will all fail against the starter code, and that is expected), implement the function to make them pass.
Look at game.html to see how validation fits into the game: when the guess is invalid, the reason is shown in the message area and the guess is not processed.
Examples
validateGuess("a", []) // { valid: true }
validateGuess("7", []) // { valid: false, reason: "not a letter" }
validateGuess("a", ["a"]) // { valid: false, reason: "already guessed" }
validateGuess("A", ["a"]) // { valid: false, reason: "already guessed" }