Skip to content

WeiQ- Sphinx #29

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

WeiQ- Sphinx #29

wants to merge 3 commits into from

Conversation

hintow
Copy link

@hintow hintow commented Nov 27, 2024

No description provided.

Copy link

@yangashley yangashley left a comment

Choose a reason for hiding this comment

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

Nice job on js-adagrams!

@@ -1,15 +1,114 @@
const LETTER_POOL = {

Choose a reason for hiding this comment

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

Nice job using capital letters to name your constant variable!

Comment on lines +30 to +35
const LetterPoolList = [];
for (const [letter, count] of Object.entries(LETTER_POOL)) {
for (let i = 0; i < count; i++) {
LetterPoolList.push(letter);
}
}

Choose a reason for hiding this comment

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

Prefer this logic be put in a helper function that you can call in drawLetters

LetterPoolList.push(letter);
}
}
const totalLetterCount = LetterPoolList.length;

Choose a reason for hiding this comment

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

Suggested change
const totalLetterCount = LetterPoolList.length;
const TOTAL_LETTER_COUNT = LetterPoolList.length;

Comment on lines +47 to +48
const lastIndex = totalLetterCount - i - 1;
[LetterPoolList[randomIndex], LetterPoolList[lastIndex]] = [LetterPoolList[lastIndex], LetterPoolList[randomIndex]]

Choose a reason for hiding this comment

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

😎 nice work swapping the randomly drawn letter to the end of the list! This a constant time operation which beats an approach that uses splice to remove drawn letters.

Comment on lines +56 to +62
const letterFreMap = {};
for (const letter of lettersInHand) {
if (!(letter in letterFreMap)) {
letterFreMap[letter] = 1;
} else {
letterFreMap[letter] += 1;
}

Choose a reason for hiding this comment

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

👍 I like the frequency map here so you can leverage the constant time look up.

Comment on lines +88 to +89
word = word.toUpperCase();
for (let char of word) {

Choose a reason for hiding this comment

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

We should use const for the looping variable char since this variable's value doesn't change in the loop.

Can also write lines 88-89 in one line:

Suggested change
word = word.toUpperCase();
for (let char of word) {
for (const char of word.toUpperCase()) {

score += SCORE_CHART[char];
}

if ([7, 8, 9, 10].includes(word.length)) {

Choose a reason for hiding this comment

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

While the time complexity of using includes in this method ends up being constant because you're always only checking that a word's length is in a list of four integers, I'd prefer this to be written like

Suggested change
if ([7, 8, 9, 10].includes(word.length)) {
if (7 <= word.length <= 10)) {

Writing the check this way feels like it conveys that you're checking the length is between x and y value.

Comment on lines +103 to +107
for (const word of words) {
if (scoreWord(word) > currentMax) {
currentMax = scoreWord(word);
currentWinWord = word;
} else if (scoreWord(word) === currentMax && currentWinWord.length != 10) {

Choose a reason for hiding this comment

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

Between lines 104-107, you invoke the scoreWord method three times. Prefer you calculate the score of the current word once and save it to a variable.

Suggested change
for (const word of words) {
if (scoreWord(word) > currentMax) {
currentMax = scoreWord(word);
currentWinWord = word;
} else if (scoreWord(word) === currentMax && currentWinWord.length != 10) {
for (const word of words) {
const wordScore = scoreWord(word);
if (wordScore > currentMax) {
currentMax = wordScore;
currentWinWord = word;
} else if (wordScore === currentMax && currentWinWord.length != 10) {

currentMax = scoreWord(word);
currentWinWord = word;
} else if (scoreWord(word) === currentMax && currentWinWord.length != 10) {
if(word.length < currentWinWord.length || word.length === 10)

Choose a reason for hiding this comment

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

Suggested change
if(word.length < currentWinWord.length || word.length === 10)
if (word.length < currentWinWord.length || word.length === 10)

@@ -120,7 +120,9 @@ describe("Adagrams", () => {
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expectScores({
'':0

Choose a reason for hiding this comment

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

Suggested change
'':0
'': 0

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