Ari - Octos - JS-Scrabble#24
Conversation
JS ScrabbleWhat We're Looking For
|
| for( let letter of wordArray ) { | ||
| switch (true) { | ||
| case /[aeioulnrst]/i.test(letter): | ||
| sum += 1; |
There was a problem hiding this comment.
I'm not a big fan of organizing the letter scores in a switch statement like this. When the data is intertwined with the code that uses it, 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}`);
}
}| tieBreaker(first, second) { | ||
| if (first.length == 7) { | ||
| return first; | ||
| } else if (second.length == 7) { |
There was a problem hiding this comment.
Good use of a helper method to clarify this logic.
| } else if (length > 7) { | ||
| throw 'Word length must be less than 7'; | ||
| } else if (length === 0) { | ||
| throw 'Word cannot be empty'; |
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}`);
}
JS Scrabble
Congratulations! You're submitting your assignment!
Comprehension Questions