-
Notifications
You must be signed in to change notification settings - Fork 20
AC2-Ocelots - Catherine Bandarchuk - JS-AdaGrams #7
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 ^_^
| let availableListOfLetters = []; | ||
| for (const letter of POOL_LETTERS_DICT) { | ||
| for (let i = 0; i < letter["count"]; ++i) { | ||
| availableListOfLetters.push(letter["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.
Really nice solution for ensuring we get the desired distribution of letters.
| @@ -1,15 +1,136 @@ | |||
| export const drawLetters = () => { | |||
| // Implement this method for wave 1 | |||
| const POOL_LETTERS_DICT = [ | |||
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.
Great use of a const
| let lettersInHand = []; | ||
| for (let i = 0; i < 10; ++i) { | ||
| let randomIndexLetter = Math.floor( | ||
| Math.random() * availableListOfLetters.length | ||
| ); | ||
| let oneLetter = availableListOfLetters[randomIndexLetter]; | ||
| availableListOfLetters.splice(randomIndexLetter, 1); | ||
| lettersInHand.push(oneLetter); | ||
| } | ||
|
|
||
| return lettersInHand; |
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 approach!
| let randomIndexLetter = Math.floor( | ||
| Math.random() * availableListOfLetters.length | ||
| ); |
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.
Unless we're in a system that is really low on memory, I would consider making a new variable to hold the result of the multiplication over indentation.
const randomFloat = Math.random() * availableListOfLetters.length;
const randomIndex = Math.floor(randomFloat);| let randomIndexLetter = Math.floor( | ||
| Math.random() * availableListOfLetters.length | ||
| ); | ||
| let oneLetter = availableListOfLetters[randomIndexLetter]; |
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 suggest using const to declare variables which shouldn't get reassigned within the current scope.
| for (let letter of input) { | ||
| const indexOfLetter = lettersInHand.indexOf(letter); | ||
| if (indexOfLetter !== -1) { | ||
| lettersInHand.splice(indexOfLetter, 1); | ||
| checkedWord.push(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.
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.
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| let user_points = 0; | ||
| const SCORE_CHART = [ |
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.
Since this is a local constant (only has scope in this function), we should use camel case for the naming scoreChart.
| for (let letterW of word) { | ||
| const letterObj = SCORE_CHART.find(({ letter }) => letter === letterW); | ||
| user_points += letterObj.value; | ||
| } |
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.
The array find function is an O(n) operation. If we structured SCORE_CHART as a javascript object where the letters are the keys and the points are the values, you could reduce the time complexity by being able to access the SCORE_CHART's values using the letters of the input word as keys.
const scoreChart = {
A: 1,
E: 1,
I: 1,
...
X: 8,
Q: 10,
Z: 10,
};
for (const letter of word) {
const letterValue = letterValues[letter];
if (letterValue != undefined) {
score += letterValue;
}
}| if (word.length >= 7) { | ||
| user_points = 8; | ||
| } | ||
| for (let letterW 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.
| let usersScore = 0; | ||
| let maxScore = 0; | ||
| let maxWord = ""; | ||
| 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.
Nice implementation to tie break in a single loop!
Didn't do optional wave 5.