-
Notifications
You must be signed in to change notification settings - Fork 20
Ocelots - Xuan Hien Pham #10
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "core-js": "^3.8.0", | ||
| "package.json": "^2.0.1", | ||
| "vorpal": "^1.12.0" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,141 @@ | ||
| const LETTER_POOL = { | ||
| A: 9, | ||
| B: 2, | ||
| C: 2, | ||
| D: 4, | ||
| E: 12, | ||
| F: 2, | ||
| G: 3, | ||
| H: 2, | ||
| I: 9, | ||
| J: 1, | ||
| K: 1, | ||
| L: 4, | ||
| M: 2, | ||
| N: 6, | ||
| O: 8, | ||
| P: 2, | ||
| Q: 1, | ||
| R: 6, | ||
| S: 4, | ||
| T: 6, | ||
| U: 4, | ||
| V: 2, | ||
| W: 2, | ||
| X: 1, | ||
| Y: 2, | ||
| Z: 1, | ||
| }; | ||
|
|
||
| const scoreChart = { | ||
| A: 1, | ||
| B: 3, | ||
| C: 3, | ||
| D: 2, | ||
| E: 1, | ||
| F: 4, | ||
| G: 2, | ||
| H: 4, | ||
| I: 1, | ||
| J: 8, | ||
| K: 5, | ||
| L: 1, | ||
| M: 3, | ||
| N: 1, | ||
| O: 1, | ||
| P: 3, | ||
| Q: 10, | ||
| R: 1, | ||
| S: 1, | ||
| T: 1, | ||
| U: 1, | ||
| V: 4, | ||
| W: 4, | ||
| X: 8, | ||
| Y: 4, | ||
| Z: 10, | ||
| }; | ||
|
|
||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| let letterFrequency = {}; | ||
| let aHandOfLetters = []; | ||
| while (aHandOfLetters.length < 10) { | ||
| let randomNum = Math.floor(Math.random() * 26); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will give us an even chance of picking any single letter in the alphabet without going over the number of each tile we have. This is slightly different than what the README asks - we won't accurately represent the distribution of tiles because we pick a letter from 1-26, when the chances of picking some letters should be higher than others. How could we update the algorithm to account for this? |
||
| let randomLetter = String.fromCharCode(randomNum + 65); | ||
|
|
||
| if ( | ||
| letterFrequency.hasOwnProperty(randomLetter) && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat way to check if a key exists! We could also use if (randomLetter in letterFrequency
&& letterFrequency[randomLetter] < LETTER_POOL[randomLetter]) {
...
} |
||
| letterFrequency[randomLetter] < LETTER_POOL[randomLetter] | ||
| ) { | ||
| aHandOfLetters.push(randomLetter); | ||
| letterFrequency[randomLetter] += 1; | ||
| } else if (!letterFrequency.hasOwnProperty(randomLetter)) { | ||
| aHandOfLetters.push(randomLetter); | ||
| letterFrequency[randomLetter] = 1; | ||
| } | ||
| } | ||
| return aHandOfLetters; | ||
| }; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| let found = 0; | ||
| for (const char of input) { | ||
| for (const i in lettersInHand) { | ||
| if (char === lettersInHand[i]) { | ||
| lettersInHand[i] = ""; | ||
| found++; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+82
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat 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. |
||
| if (found === input.length) { | ||
| return true; | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| let totalPoints = 0; | ||
| for (const char of word) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of a for...of loop. |
||
| totalPoints += scoreChart[char.toUpperCase()]; | ||
| } | ||
|
Comment on lines
+99
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could have an unexpected result if the string |
||
| if (word.length >= 7) { | ||
| totalPoints += 8; | ||
| } | ||
| return totalPoints; | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| let scoreWordsChart = {}; | ||
| for (const word of words) { | ||
| let points = scoreWord(word); | ||
| scoreWordsChart[word] = points; | ||
| } | ||
|
|
||
| let inCaseTie = []; | ||
| let val = Object.values(scoreWordsChart); | ||
| const maxScore = Math.max(...val); | ||
|
|
||
| for (const word in scoreWordsChart) { | ||
| if (maxScore === scoreWordsChart[word]) { | ||
| inCaseTie.push(word); | ||
| } | ||
| } | ||
|
|
||
| if (inCaseTie.length === 1) { | ||
| let word = inCaseTie[0]; | ||
| return { word: word, score: scoreWordsChart[word] }; | ||
| } | ||
|
|
||
| let shortestWord = inCaseTie[0]; | ||
|
|
||
| for (const word of inCaseTie) { | ||
| if (word.length === 10) { | ||
| return { word: word, score: scoreWordsChart[word] }; | ||
| } | ||
| if (shortestWord.length > word.length) { | ||
| shortestWord = word; | ||
| } | ||
| } | ||
| return { word: shortestWord, score: scoreWordsChart[shortestWord] }; | ||
| }; | ||
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
const! Since these constants at the top of the file are only accessed by one function each, it would be reasonable to place the objects inside the functions rather than as a constant. There are tradeoffs, the structure would clutter the function some, but it keeps the data as close as possible to where it's being used, and would mean that other functions who shouldn't need it couldn't access it.As a constant outside of a function, I suggest using all caps naming:
SCORE_CHART