-
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
C19 Kunzite - Abby Castillo #69
base: main
Are you sure you want to change the base?
Changes from all commits
eb56179
cefe1be
c34c0d2
966a74f
16d95b1
339623a
759c1eb
02b7cc5
e0f0044
539085a
c81e407
08b85f1
2cdc724
51d73c6
3da49dd
125a16c
dee847b
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,131 @@ | ||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
// initialize object to hold letter frequency table | ||
const letterFreqs = { | ||
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, | ||
}; | ||
|
||
// fill letterPool array with all the letters | ||
const letterPool = []; | ||
|
||
for (const [key, value] of Object.entries(letterFreqs)) { | ||
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 with this one, creating an array to hold all of the possible letter combinations with respect to their distribution! |
||
for (let i = 1; i < value + 1; i++) { | ||
letterPool.push(key); | ||
} | ||
} | ||
|
||
// draw letters into hand array | ||
let hand = []; | ||
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 |
||
|
||
for (let i = 0; i < 10; i++) { | ||
let selector = Math.random() * letterPool.length; | ||
|
||
let letter = letterPool.splice(selector, 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. nice job doubling up the array mutation and accessing the letter from the |
||
|
||
hand.push(letter); | ||
} | ||
|
||
return hand; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
let handCopy = 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. Not sure if you were trying to make a shallow copy here in an effort to avoid mutation, but just note that any mutation applied to const handCopy = [...lettersInHand];
handCopy.pop()
// now both handCopy and lettersInHand would be missing their last element. Also, |
||
|
||
for (let i = 0; i < input.length; i++) { | ||
let letter = input[i]; | ||
|
||
if (handCopy.includes(letter)) { | ||
let index = handCopy.indexOf(letter); | ||
handCopy.splice(index, 1); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
if (!word) { | ||
return 0; | ||
} | ||
|
||
const scoreChart = { | ||
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"], | ||
}; | ||
|
||
let score = 0; | ||
|
||
if (word.length >= 7) { | ||
score += 8; | ||
} | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
let letter = word[i]; | ||
for (const [key, value] of Object.entries(scoreChart)) { | ||
if (value.includes(letter.toUpperCase())) { | ||
score += parseInt(key); | ||
// console.log(score); | ||
} | ||
} | ||
} | ||
|
||
return score; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
let largestWord = ""; | ||
let largestWordScore = 0; | ||
|
||
for (let i = 0; i < words.length; i++) { | ||
let word = words[i]; | ||
let wordScore = scoreWord(word); | ||
if (wordScore > largestWordScore) { | ||
largestWord = word; | ||
largestWordScore = wordScore; | ||
} else if (wordScore === largestWordScore) { | ||
if (largestWord.length === 10) { | ||
largestWord = largestWord; | ||
} else if (word.length === 10) { | ||
largestWord = word; | ||
} else if (word.length < largestWord.length) { | ||
largestWord = word; | ||
} else if (largestWord.length < word.length) { | ||
largestWord = largestWord; | ||
} | ||
} | ||
} | ||
let result = { word: largestWord, score: largestWordScore }; | ||
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 |
||
return result; | ||
}; |
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.
Nothing wrong with it being defined here but because mappings like this can be valuable outside of being used in functions, it's often a better idea to define them in the module's main scope so that they can be exported and used elsewhere and in other functions in the same module!