Leidy Martinez - Sphinx JS Adagrams#40
Conversation
mikellewade
left a comment
There was a problem hiding this comment.
Nice work! Please let me know if there are any questions about the comments made.
| while(hand.length < 10){ | ||
| const keys = Object.keys(copiedPool); // Get an array of keys | ||
| const randomIndex = Math.floor(Math.random() * keys.length); // Generate a random index | ||
| let randomKey = keys[randomIndex]; // Get the key at the random index |
There was a problem hiding this comment.
Right now you aren't actually considering the weights of each letter, how could you change your implementation to do such a thing?
|
|
||
| // Ensure input and lettersInHand matches | ||
| const word = input.toUpperCase(); | ||
| let upperCaseHand = lettersInHand.map(item => item.toUpperCase()); |
| return false; | ||
| } | ||
| // remove it from the hand | ||
| upperCaseHand.splice(index,1); |
There was a problem hiding this comment.
These are inputs are going to be the same size (10) but what if they weren't? What if the input size varied? What would be the time complexity of your code? What CS fundamentals data structure did we learn about that is handy for keeping track of the number of occurrences?
| const SCORE_DICT = { '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 score = 0; | ||
|
|
||
| if (!word) { | ||
| return score; | ||
| } | ||
|
|
||
| const lowerCaseWord = word.toLowerCase(); | ||
| for (let letter of lowerCaseWord) { | ||
| if(SCORE_DICT[letter]){ // Check if the letter exists in SCORE_DICT | ||
| score += SCORE_DICT[letter]; | ||
| } | ||
| } | ||
|
|
||
| if (lowerCaseWord.length > 6){ | ||
| score += 8; | ||
| } | ||
| return score; | ||
| }; |
There was a problem hiding this comment.
Nice work, very legible function! I love how you had extra security checks as well.
| export const highestScoreFrom = (words) => { | ||
| let bestScore = { word: '', score: 0 }; | ||
|
|
||
| for (let word of words) { | ||
| let currentScore = scoreWord(word); | ||
|
|
||
| // If current word has a higher score, update bestScore | ||
| if (currentScore > bestScore.score) { | ||
| bestScore.score = currentScore; | ||
| bestScore.word = word; | ||
| } | ||
| // Handle tie cases | ||
| else if (currentScore === bestScore.score) { | ||
| // If the current word is shorter, update (unless the best word is 10 chars long) | ||
| if (word.length < bestScore.word.length && bestScore.word.length !== 10) { | ||
| bestScore.word = word; | ||
| } | ||
| // If the current word is 10 characters long and the best word is not, update | ||
| else if (word.length === 10 && bestScore.word.length !== 10) { | ||
| bestScore.word = word; | ||
| } | ||
| } | ||
| } | ||
| return bestScore; | ||
| }; |
There was a problem hiding this comment.
Function looks good, also the compound conditional statements look good as well!
| const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
|
|
||
| throw "Complete test by adding an assertion"; | ||
| expect(highestScoreFrom(words)).toEqual(correct); |
No description provided.