-
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
Tigers - Mica Chau #126
base: main
Are you sure you want to change the base?
Tigers - Mica Chau #126
Changes from all commits
be9eed3
692a198
4a6fab5
a3418c7
087e2f5
83b86fd
53847ae
43f74b0
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 |
---|---|---|
@@ -1,15 +1,139 @@ | ||
const LETTERPOOL = { | ||
A: 9, | ||
N: 6, | ||
B: 2, | ||
O: 8, | ||
C: 2, | ||
P: 2, | ||
D: 4, | ||
Q: 1, | ||
E: 12, | ||
R: 6, | ||
F: 2, | ||
S: 4, | ||
G: 3, | ||
T: 6, | ||
H: 2, | ||
U: 4, | ||
I: 9, | ||
V: 2, | ||
J: 1, | ||
W: 2, | ||
K: 1, | ||
X: 1, | ||
L: 4, | ||
Y: 2, | ||
M: 2, | ||
Z: 1, | ||
}; | ||
|
||
const LETTERPOINTS = { | ||
A: 1, | ||
N: 1, | ||
B: 3, | ||
O: 1, | ||
C: 3, | ||
P: 3, | ||
D: 2, | ||
Q: 10, | ||
E: 1, | ||
R: 1, | ||
F: 4, | ||
S: 1, | ||
G: 2, | ||
T: 1, | ||
H: 4, | ||
U: 1, | ||
I: 1, | ||
V: 4, | ||
J: 8, | ||
W: 4, | ||
K: 5, | ||
X: 8, | ||
L: 1, | ||
Y: 4, | ||
M: 3, | ||
Z: 10, | ||
}; | ||
|
||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
let letterPool = Object.entries(LETTERPOOL); | ||
let hand = []; | ||
let i = 0; | ||
while (i < 10) { | ||
let letterIndex = Math.floor(Math.random() * 26); | ||
if (letterPool[letterIndex][1] > 0) { | ||
hand.push(letterPool[letterIndex][0]); | ||
letterPool[letterIndex][1] -= 1; | ||
i += 1; | ||
} | ||
} | ||
Comment on lines
+63
to
+70
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. |
||
return hand; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
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. 👍 I like the helper function and it's an interesting way to do this. |
||
// Implement this method for wave 2 | ||
let handCount = substringCount(lettersInHand); | ||
let inputCount = substringCount(input.toUpperCase()); | ||
for (const letter in inputCount) { | ||
if (!handCount[letter] || inputCount[letter] > handCount[letter]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const scoreWord = (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. Love the use of |
||
// Implement this method for wave 3 | ||
let total_score = word | ||
.toUpperCase() | ||
.split("") | ||
.map((letter) => LETTERPOINTS[letter]) | ||
.reduce((accumulator, currentValue) => accumulator + currentValue, 0); | ||
if (word.length > 6) { | ||
total_score += 8; | ||
} | ||
return total_score; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
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. 👍 I like the helper function and good use of JS functions. |
||
// Implement this method for wave 4 | ||
let wordScores = {}; | ||
words.forEach((word) => (wordScores[word] = scoreWord(word))); | ||
let highestScore = Math.max(...Object.values(wordScores)); | ||
let tiedWords = []; | ||
for (const word in wordScores) { | ||
if (wordScores[word] === highestScore) { | ||
tiedWords.push(word); | ||
} | ||
} | ||
return { word: breakTie(tiedWords), score: highestScore }; | ||
}; | ||
|
||
// HELPER FUNCTIONS | ||
const substringCount = (string) => { | ||
let count = {}; | ||
for (const character of string) { | ||
if (count[character] === undefined) { | ||
count[character] = 1; | ||
} else { | ||
count[character] += 1; | ||
} | ||
} | ||
return count; | ||
}; | ||
|
||
const breakTie = (words) => { | ||
let wordLengths = {}; | ||
words.forEach((word) => { | ||
if (word.length === 10) { | ||
wordLengths[word] = 0; | ||
} else { | ||
wordLengths[word] = word.length; | ||
} | ||
}); | ||
let minLength = Math.min(...Object.values(wordLengths)); | ||
for (const word in wordLengths) { | ||
//Refactor with .filter | ||
if (wordLengths[word] === minLength) { | ||
return 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.
I like this way of scoring letters and the above object for generating the pool.