-
Notifications
You must be signed in to change notification settings - Fork 20
Ocelots - Dalia Ali #8
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
kelsey-steven-ada
left a comment
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! I’ve added some suggestions, let me know if there's anything I can clarify ^_^
| "Y", | ||
| "Z", | ||
| ]; | ||
| const lettersPoolDeepcopy = JSON.parse(JSON.stringify(lettersPool)); |
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.
Performing a copy like this is a great place to use the spread operator! What could that look like?
Since lettersPool is declared inside the function, the data structure gets re-created every time we call the function. This means we shouldn't need to copy lettersPool to ensure that changes aren't persisted across calls.
| const hand = []; | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| const random_number = Math.floor( | ||
| Math.random() * lettersPoolDeepcopy.length | ||
| ); | ||
| hand.push(lettersPoolDeepcopy[random_number]); | ||
| lettersPoolDeepcopy.splice(random_number, 1); | ||
| } | ||
| return hand; |
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.
Really nice approach!
| for (const letter of input) { | ||
| if (lettersInHand.includes(letter)) { | ||
| const index = lettersInHand.indexOf(letter); | ||
| lettersInHand.splice(index, 1); | ||
| } else { | ||
| return false; | ||
| } | ||
| } |
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 approach! If we needed to reduce the time complexity of our solution, another approach could be to build a frequency map of our hand then loop over the characters in the input, checking if the character is in our frequency map, and if it is, then check the value to see if there are still tiles left in our hand for that letter.
| const index = lettersInHand.indexOf(letter); | ||
| lettersInHand.splice(index, 1); | ||
| } else { | ||
| return false; |
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.
I like the early exit =]
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| const lettersValue = { |
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.
I like keeping these in the function that uses them so they're close to where they are referenced.
| }; | ||
| let score = 0; | ||
| word = word.toUpperCase(); | ||
| for (const 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.
Nice use of a for...of loop.
| for (const letter of word) { | ||
| score += lettersValue[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.
This could have an unexpected result if the string word has characters that aren't in lettersValue. If we have an input like "ABC DEF" then score will hold NaN (Not a Number) after the loop. If we wanted to skip non-alphabetic characters or characters that aren't in lettersValue, how could we do that?
No description provided.