-
Notifications
You must be signed in to change notification settings - Fork 710
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
Lions - Suzanne S18 #42
base: main
Are you sure you want to change the base?
Changes from all commits
af43ec2
92f843e
7906395
08e7f3a
0af5471
1988610
5bbadf0
e3c0982
17fb033
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,126 @@ | ||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
const letterBank = { | ||
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, | ||
}; | ||
function getRandomLetter(letterBank) { | ||
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. It would be good to have this declared outside of the function so it doesn't have to be recreated each time the function is called. |
||
// get an array of letters by accessing the keys in the object | ||
const letters = Object.keys(letterBank); | ||
// Math.floor rounds down to the int and Math.random picks a random number in the range of 0 - 0.999 | ||
// multiply the random number by the lenth of the letters array to return a random letter | ||
return letters[Math.floor(Math.random() * letters.length)]; | ||
} | ||
|
||
const hand = []; | ||
while (hand.length < 10) { | ||
const randomLetter = getRandomLetter(letterBank); | ||
const occurance = hand.filter((x) => x === randomLetter).length; | ||
if (occurance < letterBank[randomLetter]) { | ||
hand.push(randomLetter); | ||
} | ||
} | ||
|
||
return hand; | ||
Comment on lines
+38
to
+47
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. Excellent! |
||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
export const usesAvailableLetters = (input, drawn) => { | ||
for (let inputLetter of input) { | ||
const index = drawn.indexOf(inputLetter); | ||
if (index > -1) { | ||
drawn.splice(index, 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. We should not be removing letters from the |
||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
const scoreChart = { | ||
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. It would be good to declare this outside of the function. |
||
AEIOULNRST: 1, | ||
DG: 2, | ||
BCMP: 3, | ||
FHVWY: 4, | ||
K: 5, | ||
JX: 8, | ||
QZ: 10, | ||
}; | ||
|
||
let total = 0; | ||
for (let letter of word.toUpperCase()) { | ||
let key = Object.keys(scoreChart).filter(function (key) { | ||
return key.includes(letter); | ||
}); | ||
const score = scoreChart[key]; | ||
total += score; | ||
} | ||
Comment on lines
+74
to
+80
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. It would be more efficient to store the scores with one letter to one number value. Looking up a single key in a JS object is O(1) time complexity. Here, you need to look through every letter to find the one that matches. |
||
if (word.length > 6) { | ||
total += 8; | ||
} | ||
return total; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
// calculate score for each word and store it in a hash table, push it to an array | ||
// calculate the highest score | ||
let scoredWords = []; | ||
let maxScore = 0; | ||
|
||
for (const word of words) { | ||
let wordHash = {}; | ||
wordHash.word = word; | ||
wordHash.score = scoreWord(word); | ||
scoredWords.push(wordHash); | ||
if (wordHash.score > maxScore) { | ||
maxScore = wordHash.score; | ||
} | ||
} | ||
|
||
// tiebreakers based on word length | ||
let minLength = 10; | ||
let bestWord = ""; | ||
for (const wordObj of scoredWords) { | ||
if (wordObj.score === maxScore) { | ||
if (wordObj.word.length === 10) { | ||
bestWord = wordObj.word; | ||
{ | ||
break; | ||
} | ||
Comment on lines
+110
to
+112
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 we know this is definitely the best word, we can just do |
||
} else if (wordObj.word.length < minLength) { | ||
bestWord = wordObj.word; | ||
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. Instead of keeping track of just the best word, we can keep track of the whole |
||
minLength = wordObj.word.length; | ||
} | ||
} | ||
} | ||
|
||
// iterate over array of word objects and return the best one | ||
for (const wordObj of scoredWords) { | ||
if (wordObj.word === bestWord) { | ||
return wordObj; | ||
} | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,9 @@ describe("Adagrams", () => { | |
}); | ||
|
||
it("returns a score of 0 if given an empty input", () => { | ||
throw "Complete test"; | ||
expectScores({ | ||
"": 0, | ||
}); | ||
Comment on lines
+123
to
+125
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. Excellent! |
||
}); | ||
|
||
it("adds an extra 8 points if word is 7 or more characters long", () => { | ||
|
@@ -133,7 +135,7 @@ describe("Adagrams", () => { | |
}); | ||
}); | ||
|
||
describe.skip("highestScoreFrom", () => { | ||
describe("highestScoreFrom", () => { | ||
it("returns a hash that contains the word and score of best word in an array", () => { | ||
const words = ["X", "XX", "XXX", "XXXX"]; | ||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
|
@@ -145,7 +147,7 @@ describe("Adagrams", () => { | |
const words = ["XXX", "XXXX", "X", "XX"]; | ||
const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
|
||
throw "Complete test by adding an assertion"; | ||
expect(highestScoreFrom(words)).toEqual(correct); | ||
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. Excellent! |
||
}); | ||
|
||
describe("in case of tied score", () => { | ||
|
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.
It would be good to have this declared outside of the function so it doesn't have to be recreated each time the function is called.