-
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
Kate - Lion - js-adagrams #44
base: main
Are you sure you want to change the base?
Changes from all commits
0241fe4
aaa41f9
ee43b0c
af78b52
0adffed
1352011
603c7f7
3f4f0c3
af609fc
896887e
d923055
3a1aae5
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,122 @@ | ||
const letterDist = { | ||
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, | ||
}; | ||
|
||
export const drawLetters = () => { | ||
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! |
||
// Implement this method for wave 1 | ||
let letterPool = []; | ||
let hand = []; | ||
|
||
// create letter pool from letter distribution | ||
for (let letter in letterDist) { | ||
for (let i = 0; i < letterDist[letter]; i++) { | ||
letterPool.push(letter); | ||
} | ||
} | ||
|
||
// for (let i = 0; i < 10; i++) { | ||
while (hand.length < 10) { | ||
let randomIndex = parseInt(Math.random() * letterPool.length); | ||
let drawnLetter = letterPool[randomIndex]; | ||
hand.push(drawnLetter); | ||
letterPool.splice(randomIndex, 1); | ||
} | ||
|
||
return hand; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
for (let letter of input) { | ||
if (lettersInHand.includes(letter)) { | ||
let i = lettersInHand.indexOf(letter); | ||
lettersInHand.splice(i, 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 | ||
// create dictionary with score for each letter | ||
let letterScore = {}; | ||
const buildScoreDict = (letters, score) => { | ||
for (const letter of letters) { | ||
letterScore[letter] = score; | ||
} | ||
}; | ||
|
||
buildScoreDict(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 1); | ||
buildScoreDict(["D", "G"], 2); | ||
buildScoreDict(["B", "C", "M", "P"], 3); | ||
buildScoreDict(["F", "H", "V", "W", "Y"], 4); | ||
buildScoreDict(["K"], 5); | ||
buildScoreDict(["J", "X"], 8); | ||
buildScoreDict(["Q", "Z"], 10); | ||
Comment on lines
+66
to
+79
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 can be done by just creating the object like you do with the |
||
|
||
// calculate score | ||
word = word.toUpperCase(); | ||
|
||
let points = 0; | ||
if (word.length === 0) { | ||
return points; | ||
} else if (word.length > 6) { | ||
points += 8; | ||
} | ||
for (let letter of word) { | ||
points += letterScore[letter]; | ||
} | ||
return points; | ||
Comment on lines
+82
to
+93
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 highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
let max = 0; | ||
let highestScoreWord = ""; | ||
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. Instead of calling |
||
if (scoreWord(word) > max) { | ||
max = scoreWord(word); | ||
highestScoreWord = word; | ||
} else if (scoreWord(word) === max) { | ||
if (word.length != 10 && highestScoreWord.length != 10) { | ||
if (word.length < highestScoreWord.length) { | ||
highestScoreWord = word; | ||
} | ||
} else if (word.length === 10 && highestScoreWord.length != 10) { | ||
highestScoreWord = word; | ||
} else if (word.length != 10 && highestScoreWord.length === 10) { | ||
highestScoreWord = highestScoreWord; | ||
} else if (word.length === highestScoreWord) { | ||
highestScoreWord = highestScoreWord; | ||
Comment on lines
+110
to
+113
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 |
||
} | ||
} | ||
} | ||
const output = { | ||
word: highestScoreWord, | ||
score: max, | ||
}; | ||
return output; | ||
}; |
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.
Excellent!