-
Notifications
You must be signed in to change notification settings - Fork 169
JavaScript Adagram #143
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?
JavaScript Adagram #143
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,123 @@ | ||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
}; | ||
const letter_count = { | ||
"A" : 9, | ||
"N" : 6, | ||
"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 letter_bank = []; | ||
const drawn = []; | ||
|
||
const keyList = Object.keys(letter_count); | ||
for(let key of keyList) { | ||
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. 👀 Prefer |
||
let frequency = 0; | ||
while(frequency < letter_count[key]){ | ||
letter_bank.push(key) | ||
frequency ++ | ||
}; | ||
Comment on lines
+37
to
+41
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 know how many times this loop should run, so prefer using a for (let i = 0; i < letter_count[key]; i += 1) {
letter_bank.push(key)
}; |
||
}; | ||
|
||
for(let i = 0; i < 10; i++) { | ||
let select_char = Math.floor(Math.random() * (letter_bank.length -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.
const select_char = Math.floor(Math.random() * (letter_bank.length)); This logic selects letters with equal weighting. You have logic to reject invalid letters (letters we've already used up), but be aware that this will tend to select hands with an overrepresentation of "hard" letters, which would make it harder for the player to form words. |
||
drawn.push(letter_bank[select_char]); | ||
letter_bank.splice(select_char, 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. 👀 |
||
}; | ||
|
||
return drawn; | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
for(let char of input) { | ||
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. 👀 Prefer |
||
if(lettersInHand.includes(char)){ | ||
lettersInHand.splice(char, 1); | ||
}else{ | ||
return false; | ||
} | ||
Comment on lines
+55
to
+59
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.
Note that And keep in mind that rather than using |
||
}; | ||
|
||
return true; | ||
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
const score_card = { | ||
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. 👀 Prefer to list this out alphabetically. It's hard for a human reader to be able to tell whether all the letters are there. 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. Consider moving the score chart out of the function to put the focus on the logic. |
||
A: 1, E: 1, I: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1, | ||
D: 2, G: 2, | ||
B: 3, C: 3, M: 3, P: 3, | ||
F: 4, H: 4, V: 4, W: 4, Y: 4, | ||
K: 5, | ||
J: 8, X: 8, | ||
Q: 10, Z: 10 | ||
}; | ||
|
||
let total_score = 0 ; | ||
|
||
for(let rawChar of 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. Prefer 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.
|
||
let char = rawChar.toUpperCase(); | ||
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.
|
||
if(Object.keys(score_card).includes(char)){ | ||
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 will iterate over all the keys of the score chart (building a new list), then iterating through the list to look for |
||
total_score += score_card[char]; | ||
} | ||
}; | ||
if (word.length >= 7) { | ||
total_score += 8; | ||
} | ||
return total_score; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
let word_with_score = []; | ||
let maxScore = 0 ; | ||
let max_word = ""; | ||
let scoreObject = {}; | ||
for(let each_word of words){ | ||
let score = scoreWord(each_word) | ||
if (score in scoreObject) { | ||
scoreObject[score].push(each_word) | ||
} | ||
else { | ||
scoreObject[score] = [each_word] | ||
} | ||
Comment on lines
+96
to
+102
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 way to build up lists of tied words! |
||
if(score >= maxScore){ | ||
maxScore = score | ||
} | ||
} | ||
|
||
let wordToChoose = scoreObject[maxScore][0]; | ||
if (scoreObject[maxScore].length > 1) { | ||
if (wordToChoose.length !== 10) { | ||
for (let each_word of scoreObject[maxScore]) { | ||
if (each_word.length < wordToChoose.length) { | ||
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's a little surprising to no have the 10-letter check here as well, but the logic checks out. If the first word is 10-letter, we'll never get here. If any other word is 10-letter, we'll exit the loop. Consider teasing apart some of this logic for clarity. |
||
wordToChoose = each_word; | ||
} | ||
if (wordToChoose.length !== 10 && each_word.length === 10) { | ||
wordToChoose = each_word; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return { "word": wordToChoose, "score": maxScore}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ describe("Adagrams", () => { | |
}); | ||
|
||
it("returns a score of 0 if given an empty input", () => { | ||
throw "Complete test"; | ||
expectScores({}); | ||
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. The expectScores({
'': 0,
}); |
||
}); | ||
|
||
it("adds an extra 8 points if word is 7 or more characters long", () => { | ||
|
@@ -133,7 +133,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 +145,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.
Consider defining
letter_count
outside the function to focus on the code. Your code already does the work of making a copy of data when building the expanded letter list, so relocating it won't be a problem.