-
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
Zoisite - Muniba K. #137
base: main
Are you sure you want to change the base?
Zoisite - Muniba K. #137
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.
Great work on your first javascript project 🎉 I've added some questions & comments below, please take a look when you have a moment and reach out here or on Slack if I can clarify anything =]
let countLetters = 0; | ||
while (countLetters < frequency) { | ||
arrLetterPool.push(letter); | ||
countLetters++; | ||
} |
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.
If we didn't want to manage the count ourself, a for loop would work great here:
for (let [letter, frequency] of Object.entries(LETTER_POOL)) {
for (let i = 0; i < frequency; i++) {
arrLetterPool.push(letter);
}
}
|
||
while (drawnLetters.length < 10) { | ||
let randomLetter = Math.floor(Math.random() * arrLetterPool.length); | ||
let removedLetter = arrLetterPool.splice(randomLetter, 1)[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.
Great use for splice! Another way to get the single value from what splice returns is with array unpacking syntax:
const [removedLetter] = availablePool.splice(randomIndex, 1);
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
}; | ||
let lettersInHandCopy = Array.from(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.
Another common syntax for copying arrays is the spread operator ...
:
const lettersInHandCopy = [...lettersInHand];
const currentLetter = input[i]; | ||
//Line below is needed because the letterIndex is what is used to loop through the lettersInHandCopy array | ||
//If the currentLetter is not found, indexOf() returns -1. | ||
const letterIndex = lettersInHandCopy.indexOf(currentLetter) |
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.
indexOf
needs to scan through a list to find the element to return the index, what is the time complexity of this function overall? What strategies could we use to try and reduce the complexity?
if (word.length >= 7) { | ||
score += 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.
This could be a great place for a ternary operator:
// Ternary operator structure:
// var = (conditional) ? (value if true) : (value if false)
score = (word.length >= 7) ? (score + 8) : score
|
||
for (let letter of wordUpper) { | ||
for (let [points, letters] of Object.entries(SCORE_CHART_DICT)) { | ||
if (letters.includes(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.
If you want to use the current data formatting of SCORE_CHART_DICT
, I would strongly consider changing SCORE_CHART_DICT
to use sets for the values to store the letters for each score key so that we can check for membership in O(1) time. When they are arrays includes
will scan through each element in the list to check for membership.
if (shortestWord.length === 10) { | ||
shortestWord = shortestWord; | ||
} else if (word.length === 10) { | ||
shortestWord = word; | ||
} else if (word.length < shortestWord.length) { | ||
shortestWord = 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.
Since the result is no change for the first conditional, we could refactor to remove the assignment of shortestWord
to itself. There are many ways to approach it, one option is to use variables to represent the conditionals we need to consider:
const isBestTen = shortestWord.length === 10
const isWordTen = word.length === 10
const isWordShorter = word.length < shortestWord.length
if ((isWordTen && !isBestTen) || (isWordShorter && !isBestTen)) {
shortestWord = word;
}
No description provided.