Skip to content

C22 Phenix Luqi Xie #33

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

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

C22 Phenix Luqi Xie #33

wants to merge 5 commits into from

Conversation

shiqipaper
Copy link

No description provided.

Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

Looks good! Please review my comments, and let me know if you have any questions. Nice job!

Z: 1,
};

const SCORE_CHART = {

Choose a reason for hiding this comment

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

The order of the letters aren't important to JS, but we can make it a little easier to double check that the scores are all present and accounted for if we wrote this in alphabetical order.

const SCORE_CHART = {
  A: 1,
  B: 3,
  C: 3,
  ...
};

Comment on lines +71 to +72
[letterList[letterIndex], letterList[lastPos]] = [letterList[lastPos], letterList[letterIndex]];
drawLetters.push(letterList.pop());

Choose a reason for hiding this comment

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

Nice approach of swapping the picked letter to the end, which then allows us to pop in constant time. This makes the complexity dependent only upon the size of the hand.

Comment on lines +88 to +94
for (const letter of input.toUpperCase()) {
if (!letterFrequencyMap[letter] || letterFrequencyMap[letter] === 0) {
return false;
} else {
letterFrequencyMap[letter] -= 1;
}
}

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 to check the validity of the owrd against th hand. We can build the map in a single iteration over the hand, then validate the word in a single iteration over the word, resulting in time complexity O(m + n), where m is the size of the hand and n is the size of the word.

if (!letterFrequencyMap[letter] || letterFrequencyMap[letter] === 0) {
return false;
} else {
letterFrequencyMap[letter] -= 1;

Choose a reason for hiding this comment

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

Even though JS doesn't use indentation to control the flow of logic, we should still make sure to follow good indentation practice to help with readability. So we'd want to indent this line.

if (score > highestScore) {
highestScore = score;
bestWord = word;
} else if (score === highestScore && bestWord.length !== 10) {

Choose a reason for hiding this comment

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

Each of the nested cases would only apply if the best word wasn't already of length 10, so moving that up to this condition helps simplify the nested conditions.

Comment on lines +124 to +126
expectScores({
"": 0,
});

Choose a reason for hiding this comment

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

👍

@@ -133,7 +136,7 @@ describe("Adagrams", () => {
});
});

describe.skip("highestScoreFrom", () => {
describe("highestScoreFrom", () => {

Choose a reason for hiding this comment

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

👍

@@ -145,7 +148,8 @@ describe("Adagrams", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };

throw "Complete test by adding an assertion";
// 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.

👍

Comment on lines +99 to +102
let totalScore = 0;
for (const letter of word.toUpperCase()) {
totalScore += SCORE_CHART[letter];
}

Choose a reason for hiding this comment

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

Great use of a score lookup table to keep this simple. We could also use Array.reduce to perform this summation.

  let totalScore = word.toUpperCase().split('').reduce((acc, letter) => acc + SCORE_CHART[letter], 0);

Personally, I find the loop approach you used more readable, but the reduce approach is also popular with JS programmers.

Co-authored-by: Ansel Rognlie <[email protected]>
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