-
Notifications
You must be signed in to change notification settings - Fork 44
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
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.
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); |
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 could also use the expectScores
helper as
expectScores({
'': 0,
});
@@ -133,7 +134,7 @@ describe("Adagrams", () => { | |||
}); | |||
}); | |||
|
|||
describe.skip("highestScoreFrom", () => { | |||
describe("highestScoreFrom", () => { |
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.
👍
@@ -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); |
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.
👍
@@ -1,15 +1,116 @@ | |||
// import { LETTER_POOL } from '../test/adagrams.test.js'; |
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.
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); |
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.
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.
for (const letter of word.toUpperCase()) { | ||
totalScores += scoreChart[letter]; | ||
} |
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'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.
if (word.length > 6) { | ||
totalScores += 8; | ||
} |
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.
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) { |
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.
Prefer const
for (const word of words) {
let bestScore = 0; | ||
|
||
for (let word of words) { | ||
let score = scoreWord(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.
Prefer const
const score = scoreWord(word);
} else if (score === bestScore) { | ||
if (word.length === 10 && bestWord.length !== 10) { | ||
bestWord = word; | ||
} else if (word.length < bestWord.length && bestWord.length !== 10) { | ||
bestWord = 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.
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.
All tests of 4 waves passed. I also demo played and it worked.