Anne Watson -- Octos -- JS-Scrabble#19
Conversation
JS ScrabbleWhat We're Looking For
|
| switch (letter) { | ||
| case 'a': | ||
| case 'e': | ||
| case 'i': |
There was a problem hiding this comment.
I'm not a big fan of organizing the letter scores in a switch statement like this. It's not very DRY (you've had to type case 26 times), but more importantly the data is intertwined with the code that uses it, which means there's no easy way to reuse this information.
For example, what if the player had a hand full of tiles, and you wanted to be able to display the value of each of those letters? You would have to copy/paste this switch in that code, or at least extract it to a method.
A better approach would be to store the scores in a hash:
const LETTER_VALUES = {
a: 1,
b: 3,
// ...
}then here you could use it like this:
word.forEach((letter) => {
const value = LETTER_VALUES[letter.toLowerCase()];
if (value) {
totalScore += letter.toLowerCase();
} else {
throw new Error(`Invalid character ${letter}`);
}
}| if (word.length > 7) { | ||
| throw 'Words must be 7 letters or less'; | ||
| } | ||
|
|
There was a problem hiding this comment.
Instead of throwing a string, you should throw an instance of Error. That gives you access to stack trace information. Something like this:
if (condition) {
throw new Error(`Invalid word ${word}`);
}| test('returns false when score < 100', () => { | ||
| const player = new Scrabble.Player('test player'); | ||
| const words = [{word: 'academy', score: 65}, {word: 'cat', score: 5}, {word: 'goat', score: 5}]; | ||
| let totalScore = 0; |
There was a problem hiding this comment.
I like how explicit and intentional you are here about what the word scores should be. That makes these tests very readable.
JS Scrabble
Congratulations! You're submitting your assignment!
Comprehension Questions