Conversation
…select 10 letters
…ch word has the highest score
nancy-harris
left a comment
There was a problem hiding this comment.
Excellent job! There are some comments below on places to make the code cleaner/simpler. There is also a function that does not work as expected. See comments below. Great job!
| // const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
|
|
||
| const availableLetters = { |
There was a problem hiding this comment.
This can be declared outside of the function so it doesn't have to be recreated each time the function is called.
| const letterBowl = []; | ||
|
|
||
| Object.keys(availableLetters).forEach((key) => { | ||
| const quantity = availableLetters[key]; | ||
| for (let i = 0; i < quantity; i++) { | ||
| letterBowl.push(key); | ||
| } | ||
| }); | ||
|
|
||
| const letterHand = []; | ||
| for (let i = 0; i < 10; i++) { | ||
| const i = Math.floor(Math.random() * letterBowl.length); | ||
| const randomLetter = letterBowl[i]; | ||
| letterHand.push(randomLetter); | ||
| letterBowl.splice(i, 1); | ||
| } | ||
| return letterHand; |
| if (lettersInHand.includes(letter)) { | ||
| // remove letter from lettersInHand | ||
| const letterIndex = lettersInHand.indexOf(letter); | ||
| lettersInHand.splice(letterIndex, 1); |
There was a problem hiding this comment.
We should not be removing letters from lettersInHand. This function should only check if all of the letters in input are in lettersInHand. If we're removing letters each time we're doing this check, the user will run out of letters quickly.
| const letterIndex = lettersInHand.indexOf(letter); | ||
| lettersInHand.splice(letterIndex, 1); | ||
| } else { | ||
| submittedLetter = false; |
There was a problem hiding this comment.
Instead of having a variable to track this, we can just return false as soon as we hit this condition even once. We'll also save ourselves from needing to check the rest of input.
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| const letterValues = { |
There was a problem hiding this comment.
This can be declared outside of the function.
| const capitalizedWord = word.toUpperCase(); | ||
|
|
||
| let score = 0; | ||
|
|
||
| for (let letter = 0; letter < capitalizedWord.length; letter++) { | ||
| let wordValue = capitalizedWord[letter]; | ||
| score += letterValues[wordValue]; | ||
| } | ||
| if (capitalizedWord.length >= 7) { | ||
| score += 8; | ||
| } | ||
| return score; |
| return score; | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { |
| expectScores({ | ||
| "": 0, | ||
| }); |
| const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
|
|
||
| throw "Complete test by adding an assertion"; | ||
| expect(highestScoreFrom(words)).toEqual(correct); |
No description provided.