Skip to content
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

Tigers - Mica Chau #126

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 128 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,139 @@
const LETTERPOOL = {
A: 9,
N: 6,
B: 2,
O: 8,
C: 2,
P: 2,
D: 4,
Q: 1,
E: 12,
R: 6,
F: 2,
S: 4,
G: 3,
T: 6,
H: 2,
U: 4,
I: 9,
V: 2,
J: 1,
W: 2,
K: 1,
X: 1,
L: 4,
Y: 2,
M: 2,
Z: 1,
};

const LETTERPOINTS = {

Choose a reason for hiding this comment

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

I like this way of scoring letters and the above object for generating the pool.

A: 1,
N: 1,
B: 3,
O: 1,
C: 3,
P: 3,
D: 2,
Q: 10,
E: 1,
R: 1,
F: 4,
S: 1,
G: 2,
T: 1,
H: 4,
U: 1,
I: 1,
V: 4,
J: 8,
W: 4,
K: 5,
X: 8,
L: 1,
Y: 4,
M: 3,
Z: 10,
};

export const drawLetters = () => {
// Implement this method for wave 1
let letterPool = Object.entries(LETTERPOOL);
let hand = [];
let i = 0;
while (i < 10) {
let letterIndex = Math.floor(Math.random() * 26);
if (letterPool[letterIndex][1] > 0) {
hand.push(letterPool[letterIndex][0]);
letterPool[letterIndex][1] -= 1;
i += 1;
}
}
Comment on lines +63 to +70

Choose a reason for hiding this comment

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

⚠️ One problem here. This solution means you have an equal possibility of drawing a 'Z' and an 'A' despite there being more 'A's than 'Z's in the pool.

return hand;
};

export const usesAvailableLetters = (input, lettersInHand) => {

Choose a reason for hiding this comment

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

👍 I like the helper function and it's an interesting way to do this.

// Implement this method for wave 2
let handCount = substringCount(lettersInHand);
let inputCount = substringCount(input.toUpperCase());
for (const letter in inputCount) {
if (!handCount[letter] || inputCount[letter] > handCount[letter]) {
return false;
}
}
return true;
};

export const scoreWord = (word) => {

Choose a reason for hiding this comment

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

Love the use of map and reduce.

// Implement this method for wave 3
let total_score = word
.toUpperCase()
.split("")
.map((letter) => LETTERPOINTS[letter])
.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
if (word.length > 6) {
total_score += 8;
}
return total_score;
};

export const highestScoreFrom = (words) => {

Choose a reason for hiding this comment

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

👍 I like the helper function and good use of JS functions.

// Implement this method for wave 4
let wordScores = {};
words.forEach((word) => (wordScores[word] = scoreWord(word)));
let highestScore = Math.max(...Object.values(wordScores));
let tiedWords = [];
for (const word in wordScores) {
if (wordScores[word] === highestScore) {
tiedWords.push(word);
}
}
return { word: breakTie(tiedWords), score: highestScore };
};

// HELPER FUNCTIONS
const substringCount = (string) => {
let count = {};
for (const character of string) {
if (count[character] === undefined) {
count[character] = 1;
} else {
count[character] += 1;
}
}
return count;
};

const breakTie = (words) => {
let wordLengths = {};
words.forEach((word) => {
if (word.length === 10) {
wordLengths[word] = 0;
} else {
wordLengths[word] = word.length;
}
});
let minLength = Math.min(...Object.values(wordLengths));
for (const word in wordLengths) {
//Refactor with .filter
if (wordLengths[word] === minLength) {
return word;
}
}
};
6 changes: 3 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Adagrams", () => {
});

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

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +133,7 @@ describe("Adagrams", () => {
});
});

describe.skip("highestScoreFrom", () => {
describe("highestScoreFrom", () => {
it("returns a hash that contains the word and score of best word in an array", () => {
const words = ["X", "XX", "XXX", "XXXX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };
Expand All @@ -145,7 +145,7 @@ describe("Adagrams", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };

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

describe("in case of tied score", () => {
Expand Down