Skip to content

Phoenix - C22 - Tram Hoang #36

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 1 commit into
base: main
Choose a base branch
from
Open

Conversation

tramhn001
Copy link

@tramhn001 tramhn001 commented Nov 27, 2024

All tests of 4 waves passed. I also demo played and it worked.

@tramhn001 tramhn001 changed the title Finished 4 waves and tests Phoenix - C22 - Tram Hoang Nov 27, 2024
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!

@@ -120,8 +120,9 @@ describe("Adagrams", () => {
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expect(scoreWord("")).toBe(0);

Choose a reason for hiding this comment

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

We could also use the expectScores helper as

      expectScores({
        '': 0,
      });

@@ -133,7 +134,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 +146,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.

👍

@@ -1,15 +1,116 @@
// import { LETTER_POOL } from '../test/adagrams.test.js';

Choose a reason for hiding this comment

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

Good call to exclude this. Our implementation logic should never depend on anything from the tests.

while (lettersInHand.length < 10) {
const index = Math.floor(Math.random() * letterList.length);
lettersInHand.push(letterList[index]);
letterList.splice(index, 1);

Choose a reason for hiding this comment

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

Much like pop/remove in python, splice will need to shift values forward to close the hole left by the removed item. To avoid the internal loop this requires, we can first swap it with value to the end of the pool, then pop it. Strictly speaking, it's not even necessary to remove the value from the list, so lang as we adjusted the value we used as the list length to reflect the size of the available portion of the pool.

Comment on lines +86 to +88
for (const letter of word.toUpperCase()) {
totalScores += scoreChart[letter];
}

Choose a reason for hiding this comment

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

We'll often see summing up a result handled with the Array.reduce method.

  totalScores = word. toUpperCase().split('').reduce((acc, letter) => acc + scoreChart[letter], 0)

I tend to find the explicit loop version you used to be more readable, but reduce is pretty popular among JS afficionados.

Comment on lines +89 to +91
if (word.length > 6) {
totalScores += 8;
}

Choose a reason for hiding this comment

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

Consider creating global constants to use instead of the magic numbers, to make this code more self-documenting.

const MIN_BONUS_LENGTH = 7;
const LENGTH_BONUS = 8;

then

  if (word.length >= MIN_BONUS_LENGTH) {
    totalScores += LENGTH_BONUS;
  }

let bestWord = '';
let bestScore = 0;

for (let word of words) {

Choose a reason for hiding this comment

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

Prefer const

  for (const word of words) {

let bestScore = 0;

for (let word of words) {
let score = scoreWord(word);

Choose a reason for hiding this comment

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

Prefer const

    const score = scoreWord(word);

Comment on lines +106 to +112
} else if (score === bestScore) {
if (word.length === 10 && bestWord.length !== 10) {
bestWord = word;
} else if (word.length < bestWord.length && bestWord.length !== 10) {
bestWord = word;
}
}

Choose a reason for hiding this comment

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

Notice that both of the nested conditions requires that the winning word not have a length of 10. We could move those checks up to the outer condition, and simplify the nested checks.

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

We can also observe that the body of each of those checks is identical, so they could be combined into a single check. We could even squish them all together in that outer else, but that might be getting hard to read.

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