-
Notifications
You must be signed in to change notification settings - Fork 44
Sphinx - Vlada Rapaport #44
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
51340f3
67b0454
face2e8
d00ed1b
2de51a3
9e58771
fb79c86
fa1eed3
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,163 @@ | ||||||||||||||||||||||||||
export const drawLetters = () => { | ||||||||||||||||||||||||||
// Implement this method for wave 1 | ||||||||||||||||||||||||||
const letterPool = { | ||||||||||||||||||||||||||
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, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let letters = ""; | ||||||||||||||||||||||||||
for (const [letter, num] of Object.entries(letterPool)) { | ||||||||||||||||||||||||||
letters += letter.repeat(num); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
const randomlyHand = []; | ||||||||||||||||||||||||||
const lettersCountDict = { | ||||||||||||||||||||||||||
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.
Suggested change
Recall that Javascript doesn't have dictionaries. It has objects. |
||||||||||||||||||||||||||
A: 0, | ||||||||||||||||||||||||||
B: 0, | ||||||||||||||||||||||||||
C: 0, | ||||||||||||||||||||||||||
D: 0, | ||||||||||||||||||||||||||
E: 0, | ||||||||||||||||||||||||||
F: 0, | ||||||||||||||||||||||||||
G: 0, | ||||||||||||||||||||||||||
H: 0, | ||||||||||||||||||||||||||
I: 0, | ||||||||||||||||||||||||||
J: 0, | ||||||||||||||||||||||||||
K: 0, | ||||||||||||||||||||||||||
L: 0, | ||||||||||||||||||||||||||
M: 0, | ||||||||||||||||||||||||||
N: 0, | ||||||||||||||||||||||||||
O: 0, | ||||||||||||||||||||||||||
P: 0, | ||||||||||||||||||||||||||
Q: 0, | ||||||||||||||||||||||||||
R: 0, | ||||||||||||||||||||||||||
S: 0, | ||||||||||||||||||||||||||
T: 0, | ||||||||||||||||||||||||||
U: 0, | ||||||||||||||||||||||||||
V: 0, | ||||||||||||||||||||||||||
W: 0, | ||||||||||||||||||||||||||
X: 0, | ||||||||||||||||||||||||||
Y: 0, | ||||||||||||||||||||||||||
Z: 0, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
while (randomlyHand.length < 10) { | ||||||||||||||||||||||||||
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 to reference the integer 10 with a variable. This makes your code more readable/self documenting because it clues other devs in to the meaning of
Suggested change
|
||||||||||||||||||||||||||
const i = Math.floor(Math.random() * letters.length); | ||||||||||||||||||||||||||
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 more descriptive name for this variable, maybe like |
||||||||||||||||||||||||||
const selectedLetter = letters[i]; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (lettersCountDict[selectedLetter] < letterPool[selectedLetter]) { | ||||||||||||||||||||||||||
lettersCountDict[selectedLetter]++; | ||||||||||||||||||||||||||
randomlyHand.push(selectedLetter); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return randomlyHand; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export const usesAvailableLetters = (input, lettersInHand) => { | ||||||||||||||||||||||||||
// Implement this method for wave 2 | ||||||||||||||||||||||||||
let lettersInHandCopy = []; | ||||||||||||||||||||||||||
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.
Suggested change
A list that is declared with |
||||||||||||||||||||||||||
let wordLower = input.toLowerCase(); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (let letter of lettersInHand) { | ||||||||||||||||||||||||||
lettersInHandCopy.push(letter.toLowerCase()); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (let letter of wordLower) { | ||||||||||||||||||||||||||
Comment on lines
+81
to
+87
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. 2 notes:
Suggested change
|
||||||||||||||||||||||||||
if (!lettersInHandCopy.includes(letter)) { | ||||||||||||||||||||||||||
return false; | ||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||
lettersInHandCopy.splice(lettersInHandCopy.indexOf(letter), 1); | ||||||||||||||||||||||||||
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 true; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export const scoreWord = (word) => { | ||||||||||||||||||||||||||
// Implement this method for wave 3 | ||||||||||||||||||||||||||
const scoreDict = { | ||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||
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, | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
const bonusLengthMin = 7; | ||||||||||||||||||||||||||
const bonusLengthMax = 10; | ||||||||||||||||||||||||||
const bonusPoints = 8; | ||||||||||||||||||||||||||
Comment on lines
+126
to
+128
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. Use all caps for naming thees constant variables |
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
let totalScore = 0; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (const letter of word.toUpperCase()) { | ||||||||||||||||||||||||||
totalScore += scoreDict[letter]; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (word.length >= bonusLengthMin && word.length <= bonusLengthMax) { | ||||||||||||||||||||||||||
totalScore += bonusPoints; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return totalScore; | ||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
export const highestScoreFrom = (words) => { | ||||||||||||||||||||||||||
// Implement this method for wave 4 | ||||||||||||||||||||||||||
const tieBreakingLength = 10; | ||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||
let maxWord = words[0]; | ||||||||||||||||||||||||||
let maxScore = scoreWord(maxWord); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
for (let i = 1; i < words.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 |
||||||||||||||||||||||||||
const word = words[i]; | ||||||||||||||||||||||||||
const score = scoreWord(word); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (score > maxScore) { | ||||||||||||||||||||||||||
maxScore = score; | ||||||||||||||||||||||||||
maxWord = word; | ||||||||||||||||||||||||||
} else if (score === maxScore && maxWord.length !== tieBreakingLength) { | ||||||||||||||||||||||||||
if (word.length === tieBreakingLength || word.length < maxWord.length) { | ||||||||||||||||||||||||||
maxWord = word; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
return { word: maxWord, score: maxScore }; | ||||||||||||||||||||||||||
}; |
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.
While we use the
const
keyword to ensure the values cannot be reassigned, we should also use capital letters to convey to other devs the value is fixed.