-
Notifications
You must be signed in to change notification settings - Fork 50
Leaves - Elizabeth #43
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?
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,7 +1,104 @@ | ||
const Adagrams = { | ||
drawLetters() { | ||
// Implement this method for wave 1 | ||
// eslint-disable-next-line max-len | ||
const letters = ['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', 'M', 'M', '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']; | ||
|
||
const shuffleLetters = function(array) { | ||
//used online resources to piece this together | ||
let i = array.length - 1; | ||
while (i > 0) { | ||
const j = Math.floor(Math.random() * i); | ||
const temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
i--; | ||
} | ||
return array; | ||
}; | ||
|
||
const randomOrder = shuffleLetters(letters) | ||
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. Here, you shuffle the entire array pool of letters... and then, you take the first ten. I think you could refactor this so that you randomly pick ten letters, instead of shuffling the entire array. |
||
|
||
const createHand = function(array) { | ||
const drawn = [] | ||
while (drawn.length < 10) { | ||
const letter = array[0] | ||
drawn.push(letter); | ||
array.shift(); | ||
} | ||
return drawn; | ||
}; | ||
return(createHand(randomOrder)); | ||
|
||
}, | ||
|
||
|
||
usesAvailableLetters(input, lettersInHand) { | ||
const hand = {} | ||
let outcome = true | ||
lettersInHand.forEach(element => { | ||
if (hand[element]) { | ||
hand[element] += 1; | ||
} else { | ||
hand[element] = 1; | ||
} | ||
}); | ||
|
||
const letters = input.split('') | ||
|
||
letters.forEach(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. Consider using a loop that allows you to break out so that as soon as you encounter a |
||
if (hand[char] < 0) { | ||
outcome = false; | ||
} else if (hand[char]) { | ||
hand[char] -= 1; | ||
} else { | ||
outcome = false; | ||
} | ||
}); | ||
return outcome; | ||
|
||
}, | ||
|
||
scoreWord(input) { | ||
const letterPoints = { | ||
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. style note: move this object to ahead of all the functions, but inside the module. |
||
'A': 1, | ||
'B': 3, | ||
'C': 3, | ||
'D': 2, | ||
'E': 1, | ||
'F': 4, | ||
'G': 2, | ||
'H': 4, | ||
'I': 1, | ||
'J': 8, | ||
'K': 5, | ||
'L': 1, | ||
'M': 3, | ||
'N': 1, | ||
'O': 1, | ||
'P': 3, | ||
'Q': 10, | ||
'R': 1, | ||
'S': 1, | ||
'T': 1, | ||
'U': 1, | ||
'V': 4, | ||
'W': 4, | ||
'X': 8, | ||
'Y': 4, | ||
'Z': 10 | ||
} | ||
|
||
let total = 0 | ||
const word = input.toUpperCase() | ||
|
||
if (word.length > 6) {total += 8} | ||
|
||
const letters = word.split('') | ||
letters.forEach(char => { | ||
total += letterPoints[char] | ||
}); | ||
return total | ||
} | ||
}; | ||
|
||
// Do not remove this line or your tests will break! | ||
|
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 might store a hash of letter frequencies like this
and use it to build a pool of letters dynamically.