-
Notifications
You must be signed in to change notification settings - Fork 44
Sunitha-Sphinx-JS Adagrams #30
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
4608d7f
e1513d3
f650ca2
f55bfe4
2536bd9
4e44764
0fe8c4c
9f0f1c2
c9f9815
44cdf0a
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,150 @@ | ||
/*constructing a leeterpool object with letter as key and no. of times it can be drawn as value*/ | ||
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, | ||
}; | ||
|
||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
const drawnLetters = []; | ||
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 job using const here! |
||
//Evaluating total size of probable letters can be drawn | ||
let totalSize = Object.values(LETTERPOOL).reduce((acc, val) => acc + val, 0); | ||
let letterPoolArr = Object.entries(LETTERPOOL); | ||
let count = 0; | ||
let letterIdx; | ||
const HAND_SIZE = 10; | ||
|
||
while (drawnLetters.length < handSize) { | ||
if (totalSize === 0) { | ||
console.log('No more letters available to draw.'); | ||
break; | ||
} | ||
|
||
letterIdx = Math.random() * totalSize; | ||
for (const [letter, frequency] of letterPoolArr) { | ||
count += frequency; | ||
if (letterIdx < count) { | ||
drawnLetters.push(letter); | ||
totalSize -= 1; | ||
//updating array with already drawn letters | ||
const updatedArr = letterPoolArr.map(([l, freq]) => | ||
l === letter ? [l, freq - 1] : [l, freq] | ||
Comment on lines
+54
to
+55
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. These two lines are pretty jam packed with logic which makes it a little challenging to decipher. I'd prefer |
||
); | ||
letterPoolArr = updatedArr.filter(([l, freq]) => freq > 0); | ||
break; | ||
} | ||
} | ||
} | ||
return drawnLetters; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
const letterObj = {}; | ||
for (let letter of lettersInHand.toUpperCase()) { | ||
letterObj[letter] ? letterObj[letter]++ : (letterObj[letter] = 1); | ||
} | ||
//Utilizing object to track the letters drawn | ||
for (let letter of input.toUpperCase()) { | ||
if (letterObj[letter] === undefined || letterObj[letter] === 0) | ||
return false; | ||
else { | ||
letterObj[letter] -= 1; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
|
||
const LETTER_SCORES = { | ||
A: 1, | ||
E: 1, | ||
I: 1, | ||
O: 1, | ||
U: 1, | ||
L: 1, | ||
N: 1, | ||
R: 1, | ||
S: 1, | ||
T: 1, | ||
D: 2, | ||
G: 2, | ||
B: 3, | ||
C: 3, | ||
M: 3, | ||
P: 3, | ||
F: 4, | ||
H: 4, | ||
V: 4, | ||
W: 4, | ||
Y: 4, | ||
K: 5, | ||
J: 8, | ||
X: 8, | ||
Q: 10, | ||
Z: 10, | ||
}; | ||
word = word.toUpperCase(); | ||
let totalScore = 0; | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
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. Prefer |
||
totalScore += LETTERSCORES[word[i]]; | ||
} | ||
//Adding 8 bonus points for letters with length 7 to 10 | ||
const BONUS_POINTS = 8; | ||
if (word.length >= 7 && word.length <= 10) { | ||
totalScore += BONUS_POINTS; | ||
} | ||
|
||
return totalScore; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
const wordScores = {}; | ||
let maxWord = ''; | ||
let maxScore = 0; | ||
for (const word of words) { | ||
wordScores[word] = scoreWord(word); | ||
} | ||
for (const [word, score] of Object.entries(wordScores)) { | ||
|
||
if (score > maxScore) { | ||
maxWord = word; | ||
maxScore = score; | ||
} else if (score === maxScore) { | ||
//logic for words with score tied | ||
if (word.length === 10 && maxWord.length !== 10) maxWord = word; | ||
else if (word.length < maxWord.length && maxWord.length !== 10) | ||
maxWord = word; | ||
} | ||
} | ||
|
||
return { score: maxScore, word: maxWord }; | ||
}; |
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.
Overall, I'd prefer the logic related to creating an entire pool of letters to be in its own helper function. Something like:
Then the logic in
drawLetters
is only responsible for drawing a hand of ten letters and can invokecreateLetterPool
.How could we simplify the code for
drawLetters
? What if you generate a random index and use it to access a random letter in your letter pool array and add it todrawnLetters
. After that you can use a strategy of virtually "dividing" the list of all letters in a letter pool array into a 'used' and 'unused' side. Then you can swap a value to the used side.This can be done in 4 lines and would be a little easier to understand.