-
Notifications
You must be signed in to change notification settings - Fork 169
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
Amethyst - AnBa - Adagram JS #144
base: main
Are you sure you want to change the base?
Changes from all commits
3cd540b
d286166
86d98cb
e74b25c
1fc1855
abf2096
1adf813
0274953
1a460d1
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}/test/adagrams.test.js" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,107 @@ | ||
// global variable | ||
const LETTER_POOL = { | ||
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 | ||
} | ||
// the following line of code imports the library lodash | ||
// we use this library because we want to use the method sampleSize and the method cloneDeep | ||
const _ = require('lodash'); | ||
|
||
export const drawLetters = () => { | ||
// Implement this method for wave 1 | ||
let letterList = []; | ||
for (let [letter, freq] of Object.entries(LETTER_POOL)) { | ||
letterList.push(...Array(freq).fill(letter)); | ||
} | ||
Comment on lines
34
to
+38
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. Great work getting comfortable with JavaScript's syntax. It looks like you're getting use to looping over data and adding conditional logic ✅ |
||
return _.sampleSize(letterList, 10); | ||
}; | ||
|
||
export const usesAvailableLetters = (input, lettersInHand) => { | ||
// Implement this method for wave 2 | ||
const lettersInHand_deepcopy = _.cloneDeep(lettersInHand); | ||
for (let letter of input.toUpperCase()) { | ||
if (lettersInHand_deepcopy.includes(letter)) { | ||
lettersInHand_deepcopy.splice(lettersInHand_deepcopy.indexOf(letter), 1); | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const scoreWord = (word) => { | ||
// Implement this method for wave 3 | ||
// we are using Map instead of Object because the keys in Object must be string, symbol or number | ||
const valuesMap = new Map([ | ||
[['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1], | ||
[['D', 'G'], 2], | ||
[['B', 'C', 'M', 'P'], 3], | ||
[['F', 'H', 'V', 'W', 'Y'], 4], | ||
[['K'], 5], | ||
[['J', 'X'], 8], | ||
[['Q', 'Z'], 10] | ||
]); | ||
Comment on lines
+56
to
+64
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. Excellent choice with this data structure! |
||
|
||
let wordValue = 0; | ||
for (let letter of word) { | ||
let letterValue = 0; | ||
for (let lettersArray of valuesMap.keys()) { | ||
if (lettersArray.includes(letter.toUpperCase())) { | ||
letterValue = valuesMap.get(lettersArray); | ||
} | ||
} | ||
// we can find letterValue with just 1 line of code | ||
// but I prefer the previous approach because of its readability | ||
// let letterValue = [...valuesMap].find(([key, value]) => key.includes(letter.toUpperCase()))[1]; | ||
wordValue += letterValue; | ||
} | ||
|
||
if (word.length >= 7 && word.length <= 10) { | ||
wordValue += 8; | ||
Comment on lines
+77
to
+81
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 job translating this logic 👍🏾 |
||
} | ||
return wordValue; | ||
}; | ||
|
||
export const highestScoreFrom = (words) => { | ||
// Implement this method for wave 4 | ||
}; | ||
let result = { | ||
word: '', | ||
score: 0 | ||
}; | ||
|
||
// iterate through the array of words and choose the word with highest score | ||
// in case of tied score, we compare the words'length and determine which word will be chosen | ||
words.forEach(word => { | ||
const currentScore = scoreWord(word); | ||
|
||
if (currentScore > result.score) { | ||
result.word = word; | ||
result.score = currentScore; | ||
} else if (currentScore === result.score) { | ||
if (result.word.length !== 10 && (word.length === 10 || word.length < result.word.length)) { | ||
result.word = word; | ||
Comment on lines
+94
to
+102
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. Hi An Ba! Great job completing JS Adagrams! Congrats on finishing your first JavaScript project 🎉 Excellent choice with this scoring approach |
||
} | ||
} | ||
}); | ||
return result; | ||
} |
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.
Great job creating this global variable outside of each function so each function can access it & we can also keep the program DRY 😄