-
Notifications
You must be signed in to change notification settings - Fork 49
Leaves - Julia K #27
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: master
Are you sure you want to change the base?
Leaves - Julia K #27
Changes from all commits
730c5ef
4d6ebe0
1184084
3f5dd38
0c865dc
343d7ce
c1a3c76
3028aaf
c513b9c
4830806
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| let inputArray = [ 'J', 'I', 'L', 'T' ] | ||
| let handHash = { L: 2, T: 1, I: 2, N: 1, O: 1, J: 1, D: 2 } | ||
|
|
||
|
|
||
| inputArray.forEach(letter => { | ||
| if (handHash[letter] == null) { | ||
| return false; | ||
| } else { | ||
| handHash[letter] -= 1; | ||
| } | ||
| }); | ||
|
|
||
| console.log(handHash) | ||
|
|
||
| for (let index = 0; index < inputArray.length; index++) { | ||
| if (handHash[inputArray[index]] < 0 || handHash[inputArray[index]] == null) { | ||
| console.log(`${inputArray[index]} is false`) | ||
| } else { | ||
| console.log(`${inputArray[index]} is true`) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,87 @@ | ||
| const Adagrams = { | ||
| drawLetters() { | ||
| // Implement this method for wave 1 | ||
| const letterPoolHash = {'A':9, 'B':2, 'C':2, 'D':4, 'E':12, 'F':2, 'G':3, 'H':2, 'I':9, 'J':1, 'K':1, 'L':4, '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 letterPoolArray = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', | ||
| 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', | ||
| 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'F', 'F', 'G', 'G', | ||
| 'G', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', | ||
| 'I', 'J', 'K', 'L', 'L', 'L', 'L', 'N', 'N', 'N', 'N', | ||
| 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'P', | ||
| 'P', 'Q', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'S', 'S', | ||
| 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'U', 'U', 'U', 'U', | ||
| 'V', 'V', 'W', 'W', 'X', 'Y', 'Y', 'Z']; | ||
|
|
||
| let lettersInHand = []; | ||
|
|
||
| // turn draw random letter into an anonymous function? or a helper function? | ||
|
|
||
| // continue this loop until there are ten letters in the player hand | ||
| for (let index = 0; lettersInHand.length < 10; index++) { | ||
| // code for random courtesy of https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array | ||
| // pull a random letter from the letter pool array | ||
| let randomLetter = letterPoolArray[Math.floor(Math.random() * letterPoolArray.length)]; | ||
| // condition: if adding the letter to the player hand would exceed the count of letters in the pool, redraw | ||
|
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. yes! nice work on this logic and clear comments describing what's going on. |
||
| let count = lettersInHand.reduce((total, letter) => total + (letter === randomLetter), 0); | ||
| if (count == letterPoolHash[randomLetter]) { | ||
| randomLetter = letterPoolArray[Math.floor(Math.random() * letterPoolArray.length)]; | ||
| } else { // add the letter to player hand array | ||
| lettersInHand.push(randomLetter) | ||
| } | ||
| } | ||
| return lettersInHand | ||
| }, | ||
| }; | ||
|
|
||
| usesAvailableLetters(input, lettersInHand) { | ||
|
|
||
| // turn hand array into a hash map that we can compare to input | ||
| let handHash = {}; | ||
|
|
||
| lettersInHand.forEach(letter => { | ||
| if (handHash[letter] == null) { | ||
| handHash[letter] = 1 | ||
| } else { | ||
| handHash[letter] += 1 | ||
| } | ||
| }); | ||
|
|
||
| let inputArray = input.toUpperCase().split('') | ||
|
|
||
| // remove input letters from handHash. If letter is not in hash, return false. | ||
| inputArray.forEach(letter => { | ||
| if (handHash[letter] == null) { | ||
| return false; | ||
| } else { | ||
| handHash[letter] -= 1; | ||
| } | ||
| }); | ||
|
|
||
| // loop over handHash: if letter count in input is ever greater than letter count in hash, return false. Else return true. | ||
|
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 whether it would have been possible to do this check in the for loop above (had that not been a forEach loop)? |
||
| for (let index = 0; index < inputArray.length; index++) { | ||
| if (handHash[inputArray[index]] < 0 || handHash[inputArray[index]] == null) { | ||
| return false; | ||
| }} | ||
| return true; | ||
| }, | ||
|
|
||
| scoreWord(word) { | ||
|
|
||
| const scoreChart = {'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 wordArray = word.toUpperCase().split('') | ||
| let score = 0 | ||
|
|
||
| if (wordArray.length >= 7) { | ||
|
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 forgot |
||
| score += 8 | ||
| } | ||
|
|
||
| wordArray.forEach(letter => { | ||
| score += scoreChart[letter] | ||
| }); | ||
| return score | ||
| } | ||
| } | ||
|
|
||
| // Do not remove this line or your tests will break! | ||
| export default Adagrams; | ||
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.
This data structure works, but it's not very DRY and would be tricky to change. For example, suppose I said you had the wrong number of "I"s - you would have to do a lot of counting to solve the problem.
Instead you could use your hash of letter frequencies to create this array dynamically.