-
Notifications
You must be signed in to change notification settings - Fork 169
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
SL - Cristal Blanco S. #124
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.
Nice work getting situated in JS. Remember to keep in mind where certain built-in function calls might be hiding a loop, and try to avoid using those within an outer loop (things like splice
or includes
). Even though the small size of the data in this means the absolute performance is still fast, we should keep the algorithmic complexity in mind. Imagine what would happen if we relaxed the data restrictions to use larger tile sets or hands.
Take a look at my feedback on drawLetters
for an important point, but otherwise nicely done.
@@ -54,17 +54,17 @@ describe("Adagrams", () => { | |||
it("does not draw a letter too many times", () => { | |||
for (let i = 0; i < 1000; i++) { | |||
const drawn = drawLetters(); | |||
const letter_freq = {}; |
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 didn't really need to update this, but the linter was probably complaining about the case. 😂
@@ -120,7 +120,9 @@ describe("Adagrams", () => { | |||
}); | |||
|
|||
it("returns a score of 0 if given an empty input", () => { | |||
throw "Complete test"; | |||
expectScores({ |
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.
👍
|
||
throw "Complete test by adding an assertion"; | ||
// 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.
👍
} | ||
} | ||
while (hand.length < 10) { | ||
hand.push(letters.pop(Math.floor(Math.random() * letters.length - 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.
In JS, pop
always pops from the end. We need to use something like splice
to remove from some other position. So this currently always returns the hand
['Z', 'Y', 'Y', 'X', 'W', 'W', 'V', 'V', 'U', 'U']
Talk about hard mode!
if (lettersInHandCopy.includes(letter)) { | ||
let indexHand = lettersInHandCopy.indexOf(letter); | ||
lettersInHandCopy.splice(indexHand, 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.
Both includes
and indexOf
require iterating through the data. But indexOf
returns -1
if the sought value isn't present. So we could omit the includes
check like
const letterIndex = lettersInHandCopy.indexOf(letter)
if (letterIndex !== -1) {
lettersInHandCopy.splice(letterIndex, 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.
Observe that we are performing operations (indexOf
and splice
) which require iterating through the array of data, and that we are in a loop. So we're iterating over the data each time of the outer loop.
Consider building frequency maps for the input and the hand which requires iterating over each only once. Then rather than repeatedly removing letters from the hand array, we could compare the counts of each input letter with the available count in the hand.
} else if (word.length == "") { | ||
return 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.
This condition is unnecessary. If the word is empty, score
will be initialized to 0, the loop will not be entered (leaving it 0), and no bonus will be added, still leaving it 0.
|
||
let score = 0; | ||
|
||
for (let letter of 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
for the loop variable in a for/of
loop.
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
const lettersDict = { |
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 moving this outside the function (or wrap it aggressively) so that the function becomes shorter.
winner["score"] = score; | ||
console.log(word, score); | ||
} | ||
if (score === winner["score"] && winner["word"].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.
This should be else if
. We don't need to consider this if the word flat out beat the current winner.
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.
Otherwise, nice job teasing apart the tie breakers.
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
const letterPool = { |
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 moving this outside the function (or wrap it aggressively) so that the function becomes shorter.
No description provided.