Skip to content

Leidy Martinez - Sphinx JS Adagrams#40

Open
Leidy-Martinez wants to merge 3 commits into
Ada-C22:mainfrom
Leidy-Martinez:main
Open

Leidy Martinez - Sphinx JS Adagrams#40
Leidy-Martinez wants to merge 3 commits into
Ada-C22:mainfrom
Leidy-Martinez:main

Conversation

@Leidy-Martinez

Copy link
Copy Markdown

No description provided.

@Leidy-Martinez Leidy-Martinez changed the title Leidy Martinez - Sphinx JS Adadrams Leidy Martinez - Sphinx JS Adagrams Nov 27, 2024

@mikellewade mikellewade left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Please let me know if there are any questions about the comments made.

Comment thread src/adagrams.js
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now you aren't actually considering the weights of each letter, how could you change your implementation to do such a thing?

Comment thread src/adagrams.js

// Ensure input and lettersInHand matches
const word = input.toUpperCase();
let upperCaseHand = lettersInHand.map(item => item.toUpperCase());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of the .map function!

Comment thread src/adagrams.js
return false;
}
// remove it from the hand
upperCaseHand.splice(index,1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/adagrams.js
Comment on lines +73 to +95
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;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, very legible function! I love how you had extra security checks as well.

Comment thread src/adagrams.js
Comment on lines +97 to +121
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;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function looks good, also the compound conditional statements look good as well!

Comment thread test/adagrams.test.js
const correct = { word: "XXXX", score: scoreWord("XXXX") };

throw "Complete test by adding an assertion";
expect(highestScoreFrom(words)).toEqual(correct);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants