-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Olive - Js Adagrams - Sea Turtles #100
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great looking code, Olive! Loved the descriptive variable names and very clear code 😄 I've left some comments and suggestions, please take a look when you can and let me know if there's anything I can clarify.
// Fisher Yates shuffle | ||
for (let i = poolOfLetters.length - 1; i > 0; i--) { | ||
let j = Math.floor(Math.random() * i); | ||
let k = poolOfLetters[i]; | ||
poolOfLetters[i] = poolOfLetters[j]; | ||
poolOfLetters[j] = k; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a proper shuffling algorithm can be a significant problem to solve. We could use the array made by makePoolOfLetters
in another approach to create a hand.
If we create an empty list hand
, we could fill it by looping 10 times and in each iteration:
- create a random number between 0 and the last index of the
poolOfLetters
array - Get the letter at our random index from the
poolOfLetters
list - Push that letter onto our
hand
array - Use something like
splice
to remove the letter at our random index from thepoolOfLetters
array so the letter can't be used twice
At the end of our loop we'll have a full hand
array and can return it!
if (letter in availableLetters) { | ||
availableLetters[letter] -= 1; | ||
if (availableLetters[letter] < 0) { | ||
return false; | ||
} | ||
} else { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since javascript uses short circuit evaluation, we could condense the false cases with something like:
if (letter in availableLetters && availableLetters[letter] > 0) {
availableLetters[letter] -= 1;
} else {
return false;
}
return total; | ||
}; | ||
|
||
const tieBreaking = (top_words) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of helper functions across the file!
let shuffledLetters = shuffleLetterPool(); | ||
const letterHand = []; | ||
while (letterHand.length < 10) { | ||
letterHand.push(shuffledLetters.pop()); | ||
} | ||
return letterHand; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great use of helper functions to keep keep the function short and, more importantly, clear in what it's doing.
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
const availableLetters = buildAvailableLetters(lettersInHand); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of a frequency map!
let highestScoringWord = Object.keys(top_words)[0]; | ||
for (const word in top_words) { | ||
if (word.length === 10) { | ||
return { score: top_words[word], word: word }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the early exit as soon as we find a 10-letter word.
if ( | ||
word.length < highestScoringWord.length && | ||
highestScoringWord.length !== 10 | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice splitting this if-statement across lines for readability!
highestScoringWord = word; | ||
} | ||
} | ||
return { score: top_words[highestScoringWord], word: highestScoringWord }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is long enough that I'd consider dropping it across a few lines to make the line shorter and the structure of the object really easy to see.
for (const word in word_scores) { | ||
if (word_scores[word] !== highestScore) { | ||
delete word_scores[word]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should always be wary of modifying a data structure that we're iterating over, but this valid and a neat use of delete
with for-in to pare down a list to the highest scored rather than building up a new array!
@@ -145,7 +145,7 @@ describe("Adagrams", () => { | |||
const words = ["XXX", "XXXX", "X", "XX"]; | |||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | |||
|
|||
throw "Complete test by adding an assertion"; | |||
expect(highestScoreFrom(words)).toEqual(correct); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
No description provided.