-
Notifications
You must be signed in to change notification settings - Fork 169
Zoisite - Muniba K. #137
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?
Zoisite - Muniba K. #137
Changes from all commits
1433615
9f2d3e6
3c2642a
d8563ac
71fe450
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,128 @@ | ||
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 SCORE_CHART_DICT = { | ||
1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], | ||
2: ['D', 'G'], | ||
3: ['B', 'C', 'M', 'P'], | ||
4: ['F', 'H', 'V', 'W', 'Y'], | ||
5: ['K'], | ||
8: ['J', 'X'], | ||
10: ['Q','Z'] | ||
}; | ||
|
||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
const arrLetterPool = []; | ||
|
||
// Convert letterPool to list | ||
for (let [letter, frequency] of Object.entries(LETTER_POOL)) { | ||
let countLetters = 0; | ||
while (countLetters < frequency) { | ||
arrLetterPool.push(letter); | ||
countLetters++; | ||
} | ||
} | ||
|
||
const drawnLetters = []; | ||
|
||
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 commentThe 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); |
||
drawnLetters.push(removedLetter); | ||
} | ||
|
||
return drawnLetters; | ||
}; | ||
|
||
// Test code | ||
// console.log(drawLetters()); | ||
|
||
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 commentThe 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]; |
||
|
||
for (let i = 0; i < input.length; i++) { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (letterIndex !== -1) { | ||
lettersInHandCopy.splice (letterIndex, 1); | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
let score = 0; | ||
|
||
if (word.length >= 7) { | ||
score += 8; | ||
} | ||
Comment on lines
+86
to
+88
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 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 |
||
|
||
const wordUpper = word.toUpperCase(); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. If you want to use the current data formatting of |
||
score += parseInt(points); | ||
} | ||
} | ||
} | ||
return score; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
const scoresObj = {}; | ||
for (let word of words) { | ||
scoresObj[word] = scoreWord(word); | ||
} | ||
|
||
let highScore = 0; | ||
let shortestWord = ""; | ||
|
||
for (let [word, score] of Object.entries(scoresObj)) { | ||
if (score > highScore) { | ||
highScore = score; | ||
shortestWord = word; | ||
} else if (score === highScore) { | ||
if (shortestWord.length === 10) { | ||
shortestWord = shortestWord; | ||
} else if (word.length === 10) { | ||
shortestWord = word; | ||
} else if (word.length < shortestWord.length) { | ||
shortestWord = word; | ||
Comment on lines
+117
to
+122
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. Since the result is no change for the first conditional, we could refactor to remove the assignment of const isBestTen = shortestWord.length === 10
const isWordTen = word.length === 10
const isWordShorter = word.length < shortestWord.length
if ((isWordTen && !isBestTen) || (isWordShorter && !isBestTen)) {
shortestWord = word;
} |
||
} | ||
} | ||
} | ||
const winningScore = { "score": highScore, "word": shortestWord }; | ||
return winningScore; | ||
}; |
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: