forked from AdaGold/js-adagrams
-
Notifications
You must be signed in to change notification settings - Fork 50
Leaves - Raisah #46
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
Open
raisahv
wants to merge
5
commits into
Ada-C12:master
Choose a base branch
from
raisahv:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Leaves - Raisah #46
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6c3e37
Passing Wave 1 tests
raisahv 3c0ddba
Added function to determine if letters are available in hand
raisahv 25f906d
Add scoreWord function to Adagrams
raisahv 61c0208
Fixed functon to account for case, added extra points for long words
raisahv 639234b
Converted adagrams module to a class
raisahv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,144 @@ | ||
const Adagrams = { | ||
class Adagram { | ||
|
||
drawLetters() { | ||
// Implement this method for wave 1 | ||
}, | ||
}; | ||
const letterPoolObject = { | ||
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 | ||
} | ||
|
||
// Add letters to an array | ||
let letterPoolArray = []; | ||
|
||
for (let [letter, frequency] of Object.entries(letterPoolObject)) { | ||
let i = 0; | ||
while (i < frequency) { | ||
letterPoolArray.push(letter); | ||
i++; | ||
} | ||
} | ||
|
||
// Select random 10 letters for hand | ||
let lettersInHand = []; | ||
let j = 0; | ||
while (j < 10) { | ||
lettersInHand.push(letterPoolArray[Math.floor(Math.random() * letterPoolArray.length)]); | ||
j ++; | ||
} | ||
|
||
return lettersInHand; | ||
} | ||
|
||
usesAvailableLetters(input, lettersInHand) { | ||
let allValid = true; | ||
|
||
for (const charIndex in input) { | ||
if (lettersInHand.includes(input[charIndex])) { | ||
const handIndex = lettersInHand.indexOf(input[charIndex]); | ||
lettersInHand.splice(handIndex, 1); | ||
} else { | ||
allValid = false; | ||
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. I believe you could short circuit out of this |
||
} | ||
} | ||
|
||
return allValid; | ||
} | ||
|
||
static scoreWord(word) { | ||
const letterPointValues = { | ||
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 pointCount = 0; | ||
if (word.length > 6 ){ | ||
pointCount += 8; | ||
} | ||
|
||
// Convert word to upper case for comparison | ||
const upcaseWord = word.toUpperCase(); | ||
for (const charIndex in upcaseWord) { | ||
pointCount += letterPointValues[upcaseWord[charIndex]]; | ||
} | ||
|
||
return pointCount; | ||
} | ||
|
||
highestScoreFrom(words) { | ||
let highScoreWords = []; | ||
let highScore = 0; | ||
|
||
words.forEach( function(word) { | ||
const currentWordScore = Adagram.scoreWord(word); | ||
|
||
if (currentWordScore > highScore) { | ||
highScoreWords = [word]; | ||
highScore = currentWordScore; | ||
} else if (currentWordScore === highScore) { | ||
highScoreWords.push(word); | ||
} | ||
}) | ||
|
||
if (highScoreWords.length === 1) { | ||
return {word: highScoreWords[0], score: highScore} | ||
} else { | ||
let sortedWords = highScoreWords.sort( function(a, b){ return (b.length - a.length) }) | ||
|
||
if (sortedWords[0].length == 10){ | ||
return { word: sortedWords[0], score: highScore }; | ||
} else { | ||
return { word: sortedWords[sortedWords.length - 1], score: highScore }; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Do not remove this line or your tests will break! | ||
export default Adagrams; | ||
export default Adagram; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 work using a JS Object here!