-
Notifications
You must be signed in to change notification settings - Fork 20
Ocelots - Lisa Utsett #11
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?
Changes from all commits
b8cbca7
d4f79a3
12b0aee
21edb49
2f55885
eeef1b6
8c9f241
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "node", | ||
| "request": "launch", | ||
| "name": "Launch Program", | ||
| "skipFiles": [ | ||
| "<node_internals>/**" | ||
| ], | ||
| "program": "${workspaceFolder}/src/adagrams.js" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| }, | ||
| "dependencies": { | ||
| "core-js": "^3.8.0", | ||
| "package.json": "^2.0.1", | ||
| "vorpal": "^1.12.0" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,130 @@ | ||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| }; | ||
| "use strict"; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| }; | ||
| class Adagrams{ | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| }; | ||
| drawLetters = () => { | ||
| const letterPool = { | ||
| 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 | ||
| }; | ||
| //creates a bag of letters | ||
| let bagOfLetters = []; | ||
| for (let letter in letterPool) { | ||
| for (let i = 0; i < letterPool[letter]; ++i) { | ||
| bagOfLetters.push(letter); | ||
| } | ||
| } | ||
| const drawnLetters = []; | ||
|
Comment on lines
+16
to
+22
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. Really nice solution for ensuring we get the desired distribution of letters. |
||
|
|
||
|
|
||
| // adds ten random letters to hand | ||
| for (let i = 0; i < 10; ++i) { | ||
| let randomIndex = Math.floor((Math.random() * bagOfLetters.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. This line is getting a little nested, it isn't necessary, but I would consider moving the multiplication to its own line to make it even easier to read quickly. |
||
| let randomLetter = bagOfLetters[randomIndex]; | ||
| drawnLetters.push(randomLetter); //adds letter to hand | ||
| bagOfLetters.splice(randomIndex, 1); //removes letter from bag | ||
| } | ||
| return drawnLetters; | ||
| }; | ||
|
|
||
| usesAvailableLetters = (input, lettersInHand) => { | ||
| // dictionary with letters in hand as keys and their frequencies as values | ||
| const letterFreqMap = {}; | ||
|
|
||
| // anonymous function that is called in the forEach method below | ||
| // purpose: populates letterFreqMap | ||
| const createMap = function(letter){ | ||
| if (letter in letterFreqMap) { | ||
| letterFreqMap[letter]++; | ||
| } | ||
| else { | ||
| letterFreqMap[letter] = 1; | ||
| } | ||
| } | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| lettersInHand.forEach(createMap); | ||
|
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. Cool use of a nested function to build the frequency map! |
||
|
|
||
| // loops through every letter in input | ||
| // checks if letter is in hand | ||
| // if letter is there BUT there are no available letters left, return false | ||
| // if letter is there BUT there are available letters left, decrement the frequency of the letter by 1 | ||
| // if letter is not there, return false | ||
| for (let i = 0; i < input.length; ++i) { | ||
| if (lettersInHand.includes(input[i])){ | ||
|
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. Something to consider for complexity, letterValue = lettersInHand[input[i]]
if (letterValue === undefined || letterValue === 0) {
return false;
}
letterFreqMap[input[i]]--; |
||
| if (letterFreqMap[input[i]] === 0) { | ||
| return false; | ||
| } | ||
| else { | ||
| letterFreqMap[input[i]]--; | ||
| } | ||
| } | ||
| else { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
|
|
||
| }; | ||
|
|
||
| scoreWord = (word) => { | ||
| const scoreChart = { | ||
| 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 > 0) { | ||
| for (let letter in 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. We can iterate over the letters in for (const letter of word) {
score += scoreChart[letter.toUpperCase()];
}If we will not be reassigning the loop variable |
||
| score += scoreChart[word[letter].toUpperCase()]; | ||
| } | ||
|
Comment on lines
+87
to
+89
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 could have an unexpected result if the string |
||
| if (word.length >=7){ | ||
| score += 8; | ||
| } | ||
| } | ||
| return score; | ||
|
Comment on lines
+85
to
+94
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 structure causes the "happy path" or intended flow of our code to be indented. I suggest treating the length check like a guard clause to make the main flow of the code more prominent. if (word.length > 0) {
return 0;
}
score = 0;
for (let letter in word){
score += scoreChart[word[letter].toUpperCase()];
}
if (word.length >=7){
score += 8;
}
return score; |
||
| }; | ||
|
|
||
| highestScoreFrom = (words) => { | ||
| let highestWordAndScore = ["", 0]; | ||
| let score = 0; | ||
| for (let i in words) { | ||
|
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 implementation to tie break in a single loop! |
||
| score = this.scoreWord(words[i]); | ||
| if (score > highestWordAndScore[1]) { | ||
| highestWordAndScore = [words[i], score]; | ||
| } | ||
| // check if scores are tied | ||
| else if (score === highestWordAndScore[1]) { | ||
| // check if the lengths of both words are the same | ||
| // if they are, do nothing | ||
| if (words[i].length != highestWordAndScore[0].length) { | ||
| // if they arent, check if word is 10 letters long | ||
| if (words[i].length === 10) { | ||
| highestWordAndScore = [words[i], score]; | ||
| } | ||
| // if not, check if word with highest score is not 10 letters long | ||
| else if (highestWordAndScore[0].length != 10) { | ||
| // if not, check which word has fewer letters | ||
| if (words[i].length < highestWordAndScore[0].length) { | ||
| highestWordAndScore = [words[i], score]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return {"score":highestWordAndScore[1], "word":highestWordAndScore[0]}; | ||
| }; | ||
| }; | ||
|
|
||
| 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.
Tiny style nitpick: This is not a typical structure we see for objects/dictionaries. It takes up less lines, but we need to carefully read each line to be aware of what keys/values are on each. Though it takes more space, I suggest dropping each key/value pair to a new line to make them very easy and quick to read, unless you are on a team that has expressed a different styling that should be used.