-
Notifications
You must be signed in to change notification settings - Fork 44
C22 Sphinx Somy C #28
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: main
Are you sure you want to change the base?
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,15 +1,107 @@ | ||
const letterPool = { | ||
'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 letterValue = new Map([ | ||
[['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1], | ||
[['D', 'G'], 2], | ||
[['B', 'C', 'M', 'P'], 3], | ||
[['F', 'H', 'V', 'W', 'Y'], 4], | ||
[['K'], 5], | ||
[['J', 'X'], 8], | ||
[['Q', 'Z'], 10] | ||
]); | ||
|
||
|
||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
|
||
let lettersList = []; | ||
for (const [letter, count] of Object.entries(letterPool)) { | ||
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 work finding the javascript version of |
||
for (let i = 0; i < count; i++) { | ||
lettersList.push(letter); | ||
} | ||
} | ||
|
||
let hand = []; | ||
let temporaryLettersList = [...lettersList]; | ||
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, love to see the spread syntax being used! |
||
|
||
for (let i = 0; i < 10; i++) { | ||
const randomIndex = Math.floor(Math.random() * temporaryLettersList.length); | ||
const randomLetter = temporaryLettersList.splice(randomIndex, 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. These are letter counts will the same size but what if they weren't? What if the input size varied? What would be the time complexity of your code? What CS fundamentals data structure did we learn about that is handy for keeping track of the number of occurrences? |
||
hand.push(randomLetter); | ||
} | ||
return hand; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
for (let i = 0; i < input.length; i++) { | ||
const letter = input[i]; | ||
|
||
if (!lettersInHand.includes(letter)) { | ||
return false; | ||
} | ||
|
||
const index = lettersInHand.indexOf(letter); | ||
lettersInHand.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. You are currently mutating a mutable object, if the caller of the function is not expecting this it could lead to some difficult to debug errors. |
||
} | ||
return true; | ||
}; | ||
|
||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
if (word.length === 0){ | ||
return 0; | ||
} | ||
|
||
let totalScore = 0 | ||
for (let i = 0; i < word.length; i++) { | ||
let letter = word[i].toUpperCase(); | ||
|
||
for (let [letters, score] of letterValue) { | ||
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 you used a |
||
if (letters.includes(letter)) { | ||
totalScore += score; | ||
break; | ||
} | ||
} | ||
} | ||
if (word.length > 6){ | ||
totalScore += 8 | ||
} | ||
return totalScore | ||
Comment on lines
+55
to
+73
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 function all around! |
||
}; | ||
|
||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
|
||
if (words.length === 0) { | ||
return 0; | ||
} | ||
|
||
let winner = { word: '', score: 0 }; | ||
|
||
for (let word of words) { | ||
let currentScore = scoreWord(word); | ||
|
||
if (currentScore > winner.score) { | ||
winner = { word, score: currentScore }; | ||
} | ||
|
||
else if (currentScore === winner.score) { | ||
if (word.length === 10 && winner.word.length !== 10) { | ||
winner = { word, score: currentScore }; | ||
} | ||
|
||
else if ( | ||
word.length !== 10 && | ||
winner.word.length !== 10 && | ||
word.length < winner.word.length | ||
) { | ||
winner = { word, score: currentScore }; | ||
} | ||
} | ||
} | ||
Comment on lines
+85
to
+105
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. Your nested statements look good, easy to follow! |
||
return winner; | ||
}; |
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. ⭐️ |
||
}); | ||
|
||
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); | ||
}); | ||
|
||
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.
Nice to see you using different constructors and getting familiar with them!