Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

olive-lavine
Copy link

No description provided.

Copy link

@kelsey-steven-ada kelsey-steven-ada left a 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.

Comment on lines +71 to +77
// 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;
}

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:

  1. create a random number between 0 and the last index of the poolOfLetters array
  2. Get the letter at our random index from the poolOfLetters list
  3. Push that letter onto our hand array
  4. Use something like splice to remove the letter at our random index from the poolOfLetters 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!

Comment on lines +106 to +113
if (letter in availableLetters) {
availableLetters[letter] -= 1;
if (availableLetters[letter] < 0) {
return false;
}
} else {
return false;
}

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;
}

MDN docs on && operator and short circuit evaluation

return total;
};

const tieBreaking = (top_words) => {

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!

Comment on lines +82 to +88
let shuffledLetters = shuffleLetterPool();
const letterHand = [];
while (letterHand.length < 10) {
letterHand.push(shuffledLetters.pop());
}
return letterHand;
};

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);

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 };

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.

Comment on lines +135 to +138
if (
word.length < highestScoringWord.length &&
highestScoringWord.length !== 10
) {

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 };

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.

Comment on lines +155 to +159
for (const word in word_scores) {
if (word_scores[word] !== highestScore) {
delete word_scores[word];
}
}

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants