-
Notifications
You must be signed in to change notification settings - Fork 50
Leaves - Alice #33
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: master
Are you sure you want to change the base?
Leaves - Alice #33
Changes from all commits
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,7 +1,156 @@ | ||
const Adagrams = { | ||
drawLetters() { | ||
// Implement this method for wave 1 | ||
let letterPool = ( | ||
"A".repeat(9) + | ||
"B".repeat(2) + | ||
"C".repeat(2) + | ||
"D".repeat(4) + | ||
"E".repeat(12) + | ||
"F".repeat(2) + | ||
"G".repeat(3) + | ||
"H".repeat(2) + | ||
"I".repeat(9) + | ||
"J" + | ||
"K" + | ||
"L".repeat(4) + | ||
"M".repeat(2) + | ||
"N".repeat(6) + | ||
"O".repeat(8) + | ||
"P".repeat(2) + | ||
"Q" + | ||
"R".repeat(6) + | ||
"S".repeat(4) + | ||
"T".repeat(6) + | ||
"U".repeat(4) + | ||
"V".repeat(2) + | ||
"W".repeat(2) + | ||
"X" + | ||
"Y".repeat(2) + | ||
"Z" | ||
).split(""); | ||
let hand = []; | ||
|
||
for (let i = 0; i < 10; 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. You're modifying your |
||
letterPool.sort(() => Math.random() - 0.5); | ||
hand.push(letterPool.pop()); | ||
} | ||
|
||
return hand; | ||
}, | ||
|
||
usesAvailableLetters(input, lettersInHand) { | ||
let split = input.toUpperCase().split(""); | ||
let copy = lettersInHand.slice(0); | ||
|
||
for (let letter in split) { | ||
if (copy.includes(split[letter])) { | ||
copy.splice(copy.indexOf(split[letter]), 1); | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, | ||
|
||
scoreWord(word) { | ||
let split = word.toUpperCase().split(""); | ||
let points = 0; | ||
|
||
for (let letter in split) { | ||
switch (split[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. Good approach using a switch/case conditional! You probably could refactor this to use an object (a hash) though. |
||
case "A": | ||
case "E": | ||
case "I": | ||
case "O": | ||
case "U": | ||
case "L": | ||
case "N": | ||
case "R": | ||
case "S": | ||
case "T": | ||
points += 1; | ||
break; | ||
case "D": | ||
case "G": | ||
points += 2; | ||
break; | ||
case "B": | ||
case "C": | ||
case "M": | ||
case "P": | ||
points += 3; | ||
break; | ||
case "F": | ||
case "H": | ||
case "V": | ||
case "W": | ||
case "Y": | ||
points += 4; | ||
break; | ||
case "K": | ||
points += 5; | ||
break; | ||
case "J": | ||
case "X": | ||
points += 8; | ||
break; | ||
case "Q": | ||
case "Z": | ||
points += 10; | ||
break; | ||
} | ||
} | ||
|
||
if (split.length >= 7) { | ||
points += 8; | ||
} | ||
|
||
return points; | ||
}, | ||
|
||
highestScoreFrom(words) { | ||
let wordScore = []; | ||
|
||
for (let word in words) { | ||
let wordObj = { word: words[word], score: this.scoreWord(words[word]) }; | ||
|
||
wordScore.push(wordObj); | ||
} | ||
|
||
let max = 0; | ||
wordScore.forEach(word => { | ||
if (word.score > max) { | ||
max = word.score; | ||
} | ||
}); | ||
|
||
let winners = []; | ||
wordScore.forEach(word => { | ||
if (word.score === max) { | ||
winners.push(word); | ||
} | ||
}); | ||
|
||
let tenChar = []; | ||
winners.forEach(word => { | ||
if (word.word.length === 10) { | ||
tenChar.push(word); | ||
} | ||
}); | ||
|
||
if (tenChar.length > 0) { | ||
return tenChar[0]; | ||
} else { | ||
let minWinners = winners[0]; | ||
|
||
winners.forEach(word => { | ||
if (word.word.length < minWinners.word.length) { | ||
minWinners = word; | ||
} | ||
}); | ||
return minWinners; | ||
} | ||
} | ||
}; | ||
|
||
// Do not remove this line or your tests will break! | ||
|
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.
This should be a
const
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.
Consider first creating an object that represents letter frequencies and then using this object to build your array.