Conversation
bukunmig
commented
Dec 9, 2022
- Reviewed tests and can't see exceptions. Not sure why test is failing.
anselrognlie
left a comment
There was a problem hiding this comment.
Your submitted waves look good. Keep the algorithmic complexity in mind =, since just like python, many built-in JS array methods are hiding loops that we need to be aware of and may want to avoid when we're already within another loop. Please be sure to ping me when you've re-submitted with the revisions for the other waves so that I can review those as well.
|
|
||
| it("returns a score of 0 if given an empty input", () => { | ||
| throw "Complete test"; | ||
| expectScores ({ |
| const correct = { word: "XXXX", score: scoreWord("XXXX") }; | ||
|
|
||
| throw "Complete test by adding an assertion"; | ||
| expect(highestScoreFrom(words)).toEqual(correct); |
| let availableLetters = []; | ||
| let hand = []; | ||
|
|
||
| for (const [key, value] of Object.entries(letterPool)) { |
There was a problem hiding this comment.
Consider using names more descriptive of what the key and value represent, such as letter and count.
src/adagrams.js
Outdated
| let letter = availableLetters[Math.floor(Math.random() * availableLetters.length)]; | ||
| hand.push(letter); | ||
| let index = availableLetters.indexOf(letter); |
There was a problem hiding this comment.
Alternatively, we could organize it this way and avoid needing to find the index
let index = Math.floor(Math.random() * availableLetters.length);
let letter = availableLetters[index];
hand.push(letter);| let letter = availableLetters[Math.floor(Math.random() * availableLetters.length)]; | ||
| hand.push(letter); | ||
| let index = availableLetters.indexOf(letter); | ||
| availableLetters.splice(index, 1); |
There was a problem hiding this comment.
Note that this has the same time costs as remove in python. That is, anything after the data that was removed needs to be shifted to fill the gap in the array, resulting in a O(n) time complexity with respect to the list length. Since we're working with fixed lenths (26 letters a certain number of times each, and a 10 letter hand), we dont need to worry too much about the absolute performance, but in general we want to find approaches that don't perform linear complexity operations within a loop.
src/adagrams.js
Outdated
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| }; | ||
| // wave 2 |
There was a problem hiding this comment.
👀 Let me know when you've completed your rework on wave 2
src/adagrams.js
Outdated
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| }; | ||
| // TODO: REDO Simple option |
There was a problem hiding this comment.
👀 Let me know when you've completed your re-work on wave 4
| // wave 2 | ||
|
|
||
|
|
||
| export const scoreWord = (word) => { |
anselrognlie
left a comment
There was a problem hiding this comment.
Thanks for updating your submission. Looks good!
| // - Returns `true` if every letter in the `input` word is available (in the right quantities) in the `lettersInHand` | ||
| // - Returns `false` if not; if there is a letter in `input` that is not present in the `lettersInHand` or has too much of compared to the `lettersInHand` | ||
|
|
||
| lettersInHand = [...lettersInHand]; |
There was a problem hiding this comment.
👍 Since we're splicing values out of the hand, we need this copy so that we don't destroy the passed in hand.
| if(lettersInHand.includes(letter)) { | ||
| let indexLettersInHand = lettersInHand.indexOf(letter); | ||
| lettersInHand.splice(indexLettersInHand, 1); |
There was a problem hiding this comment.
Notice that we can use the result from indexOf itself to tell us whether the value is in the array, since it returns -1 if it is not. So we could wrrite this as
const indexLettersInHand = lettersInHand.indexOf(letter);
if (indexLettersInHand !== -1) {
lettersInHand.splice(indexLettersInHand, 1);There was a problem hiding this comment.
This approcah (removing the values from from the hand copy as we use them) is going to be a quadratic O(n^2) approach (since the splice, like a remove, must shift everything else forward as things get removes). We could still think about building a frequency table of letters to determine whether the word can be made with the hand in linear time.
| winningWordAndScore ['score'] = value; | ||
| }; | ||
|
|
||
| if (value === winningWordAndScore['score'] && winningWordAndScore['word'].length < 10) { |
There was a problem hiding this comment.
The tie breaker conditions can be an else if, since if the new score beat the old score outright, there's no need to check any of the tie breaking code.
| score: Object.values(wordsAndScores)[0] | ||
| }; | ||
|
|
||
| for (const[key, value] of Object.entries(wordsAndScores)) { |
There was a problem hiding this comment.
Consider using more descriptive names for the key and value (maybe word and score) so we don't have to remember that the key is the word and the value is the score.
| // Implement this method for wave 3 | ||
| // wave 3 | ||
| let score = 0; | ||
| for (let letter of word) { |
There was a problem hiding this comment.
Prefer to use const for the loop control variable (letter).