forked from AdaGold/js-adagrams
-
Notifications
You must be signed in to change notification settings - Fork 50
Leaves--Janice #39
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
jaitch
wants to merge
15
commits into
Ada-C12:master
Choose a base branch
from
jaitch: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--Janice #39
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e2cea85
initiate
janicewhuang edca99a
add code to put letters in hash then into array but not yet running
janicewhuang 82f433e
make progress
janicewhuang d89cd82
finish usesAvailableLetters function
janicewhuang e43ff30
fix typos
janicewhuang a9d067a
fix functions in object
janicewhuang d172ace
try a complicated way to calulate score including hash-like object wi…
janicewhuang 6923a0a
switch methods for calculating score, remember to return, tests passing
janicewhuang 2ce4727
all tests passing, through required waves
janicewhuang 0a3ba9d
get halfway through optional wave 4
janicewhuang e0bc5fa
change storage of words from object to array
janicewhuang 688f585
refactor
janicewhuang 4cb1d4b
all tests passing, hooray
janicewhuang 4e182db
apply finishing touches
janicewhuang 6c776e0
take out console.log line
janicewhuang 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,176 @@ | ||
import { objectExpression } from "@babel/types"; | ||
|
||
const makeLetters = function() { | ||
const alphabetObject = { | ||
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 alphabetArray = []; | ||
for (let key in alphabetObject) { | ||
let i = alphabetObject[key]; | ||
while (i > 0) { | ||
alphabetArray.push(key); | ||
i -= 1; | ||
} | ||
} | ||
return alphabetArray; | ||
}; | ||
|
||
// adapted from Fisher-Yates Shuffle | ||
const shuffle = function(array) { | ||
let currentIndex = array.length, temporaryValue, randomIndex; | ||
while (0 !== currentIndex) { | ||
randomIndex = Math.floor(Math.random() * currentIndex); | ||
currentIndex -= 1; | ||
temporaryValue = array[currentIndex]; | ||
array[currentIndex] = array[randomIndex]; | ||
array[randomIndex] = temporaryValue; | ||
} | ||
return array; | ||
}; | ||
|
||
const scoreWord = function(word) { | ||
const scoreboard = { | ||
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 score = 0; | ||
if (word.length >= 7) { | ||
score += 8; | ||
}; | ||
word = word.toUpperCase().split(''); | ||
word.forEach((letter) => { | ||
if (Object.keys(scoreboard).includes(letter)) { | ||
score += scoreboard[letter]; | ||
} | ||
}) | ||
return score; | ||
}; | ||
|
||
const Adagrams = { | ||
makeLetters, | ||
shuffle, | ||
|
||
drawLetters() { | ||
// Implement this method for wave 1 | ||
let shuffled = this.shuffle(this.makeLetters()); | ||
let lettersInHand = shuffled.slice(0, 10); | ||
return lettersInHand; | ||
}, | ||
}; | ||
|
||
usesAvailableLetters(input, lettersInHand) { | ||
// put drawn letters into a hash-like object | ||
let handCollection = {} | ||
lettersInHand.forEach((letter) => { | ||
if (handCollection[letter] == null) { | ||
handCollection[letter] = 1; | ||
} | ||
else { | ||
handCollection[letter] += 1; | ||
} | ||
}) | ||
// iterate through input word and check against hash-like object | ||
let letterIndex = 0; | ||
input = input.toUpperCase().split('') | ||
while (letterIndex < input.length) { | ||
if (Object.keys(handCollection).includes(input[letterIndex])) { | ||
handCollection[input[letterIndex]] -= 1; | ||
if (handCollection[input[letterIndex]] < 0) { | ||
return false; | ||
} | ||
} | ||
else if (!Object.keys(handCollection).includes(input[letterIndex])) { | ||
return false; | ||
} | ||
letterIndex += 1; | ||
} | ||
return true | ||
}, | ||
|
||
scoreWord, | ||
|
||
highestScoreFrom(words) { | ||
let wordCollection = []; | ||
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 comments to outline your logic here. There's a lot going on, there may be an opportunity for a helper function. |
||
let maxPoints = 0; | ||
words.forEach((word) => { | ||
let score = this.scoreWord(word); | ||
wordCollection.push({word: word, score: score}); | ||
if (score > maxPoints) { | ||
maxPoints = score; | ||
}; | ||
}); | ||
let winners = [] | ||
wordCollection.forEach((object) => { | ||
if (Object.values(object).includes(maxPoints)) { | ||
winners.push(object); | ||
} | ||
}) | ||
if (winners.length === 1) { | ||
return winners[0]; | ||
} | ||
let wordLength = 10; | ||
let shortest = null; | ||
let index = 0; | ||
while (index < winners.length) { | ||
if ((winners[index].word).length === 10) { | ||
return winners[index]; | ||
} | ||
if ((winners[index].word).length < wordLength) { | ||
wordLength = (winners[index].word).length; | ||
shortest = (winners[index]); | ||
} | ||
index += 1; | ||
} | ||
return shortest; | ||
} | ||
} | ||
|
||
// Do not remove this line or your tests will break! | ||
export default Adagrams; |
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.
Here, you shuffle the entire array pool of letters... and then, you take the first ten. Consider refactoring this so that you randomly pick ten letters, instead of shuffling the entire array.