-
Notifications
You must be signed in to change notification settings - Fork 103
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
Nadia Sarkissian Zoisite #79
base: main
Are you sure you want to change the base?
Changes from 7 commits
0e2c432
ef7726e
4160825
66ea49c
889db58
eda1647
41e2712
e169e5e
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,102 @@ | ||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
}; | ||
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 shuffleArray = array => { | ||
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 use of a nested function! |
||
for (let i = array.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
const temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
return array | ||
} | ||
|
||
let letterBag = []; | ||
for (const letter in LETTER_POOL) { | ||
for (let i = 0; i < LETTER_POOL[letter]; i++) { | ||
letterBag.push(letter); | ||
} | ||
} | ||
letterBag = shuffleArray(letterBag); | ||
const playerHand = letterBag.slice(0, 10); | ||
return playerHand; | ||
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 this is a pretty short statement and we don't alter |
||
} | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
}; | ||
let letterHashTable = {}; | ||
for (let letter of lettersInHand) { | ||
if (letter in letterHashTable){ | ||
++letterHashTable[letter]; | ||
} else { | ||
letterHashTable[letter] = 1; | ||
} | ||
} | ||
|
||
for (let letter of input) { | ||
const letterIsUnavailable = (letterHashTable[letter] === 0) | ||
|| !(letter in letterHashTable); | ||
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. When dropping a statement across lines, I recommend indenting the continuations one further than the initial line to make it visually clear they are related and the continuations aren't their own new statements. 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 always forget what the best way to do the indentation is! I knew this looked funny. |
||
|
||
if (letterIsUnavailable) { | ||
return false; | ||
} else { | ||
--letterHashTable[letter]; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
const LETTER_SCORE = { | ||
'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 | ||
}; | ||
|
||
word = word.toUpperCase() | ||
let score = 0; | ||
|
||
for (let letter of word){ | ||
score += LETTER_SCORE[letter]; | ||
} | ||
|
||
if (word.length >= 7) { | ||
score += 8; | ||
} | ||
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 |
||
return score; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
}; | ||
let winningWord = { | ||
word: null, | ||
score: null | ||
} | ||
|
||
for (let word of 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. Great tie breaking in a single loop! 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. you said the same thing last time! :P |
||
let thisWord = { | ||
word: word, | ||
score: scoreWord(word) | ||
} | ||
|
||
if (thisWord.score > winningWord.score){ | ||
winningWord = thisWord | ||
} else if (thisWord.score === winningWord.score){ | ||
if (winningWord.word.length === 10){ | ||
continue; | ||
} else if (thisWord.word.length === 10 | ||
|| thisWord.word.length < winningWord.word.length){ | ||
winningWord = thisWord; | ||
} | ||
} | ||
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 continue. There are many ways to approach it, one option is to use variables to represent the conditionals we need to consider: const isBestTen = winningWord.word.length === 10
const isWordTen = thisWord.word.length === 10
const isWordShorter = thisWord.word.length < winningWord.word.length
if ((isWordTen && !isBestTen) || (isWordShorter && !isBestTen)) {
winningWord = thisWord;
} 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 love how you set up these complex conditionals! Thank you for this. My spidey sense started tingling ISO a refactor when I had one line that was just "continue" |
||
} | ||
return winningWord; | ||
} |
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.
Since this is a local variable of a function, the best practice is to use camel case for the naming:
letterPool
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.
Ohh, I kept it in all-caps since we treated it like a constant variable the first time. I wasn't confident about doing all-caps here instead of camelCase.