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

Lions - Suzanne S18 #42

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
121 changes: 116 additions & 5 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,126 @@
export const drawLetters = () => {
// Implement this method for wave 1
const letterBank = {

Choose a reason for hiding this comment

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

It would be good to have this declared outside of the function so it doesn't have to be recreated each time the function is called.

A: 9,
B: 2,
C: 2,
D: 4,
E: 12,
F: 2,
G: 3,
H: 2,
I: 9,
J: 1,
K: 1,
L: 4,
M: 2,
N: 6,
O: 8,
P: 2,
Q: 1,
R: 6,
S: 4,
T: 6,
U: 4,
V: 2,
W: 2,
X: 1,
Y: 2,
Z: 1,
};
function getRandomLetter(letterBank) {

Choose a reason for hiding this comment

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

It would be good to have this declared outside of the function so it doesn't have to be recreated each time the function is called.

// get an array of letters by accessing the keys in the object
const letters = Object.keys(letterBank);
// Math.floor rounds down to the int and Math.random picks a random number in the range of 0 - 0.999
// multiply the random number by the lenth of the letters array to return a random letter
return letters[Math.floor(Math.random() * letters.length)];
}

const hand = [];
while (hand.length < 10) {
const randomLetter = getRandomLetter(letterBank);
const occurance = hand.filter((x) => x === randomLetter).length;
if (occurance < letterBank[randomLetter]) {
hand.push(randomLetter);
}
}

return hand;
Comment on lines +38 to +47

Choose a reason for hiding this comment

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

Excellent!

};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
export const usesAvailableLetters = (input, drawn) => {
for (let inputLetter of input) {
const index = drawn.indexOf(inputLetter);
if (index > -1) {
drawn.splice(index, 1);

Choose a reason for hiding this comment

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

We should not be removing letters from the drawn array. All this function should do is check to see if all of the letters in input are in drawn. If letters are being removed every time we're just checking to see if the input word is an anagram, we'll run out of letters pretty quickly.

} else {
return false;
}
}
return true;
};

export const scoreWord = (word) => {
// Implement this method for wave 3
const scoreChart = {

Choose a reason for hiding this comment

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

It would be good to declare this outside of the function.

AEIOULNRST: 1,
DG: 2,
BCMP: 3,
FHVWY: 4,
K: 5,
JX: 8,
QZ: 10,
};

let total = 0;
for (let letter of word.toUpperCase()) {
let key = Object.keys(scoreChart).filter(function (key) {
return key.includes(letter);
});
const score = scoreChart[key];
total += score;
}
Comment on lines +74 to +80

Choose a reason for hiding this comment

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

It would be more efficient to store the scores with one letter to one number value. Looking up a single key in a JS object is O(1) time complexity. Here, you need to look through every letter to find the one that matches.

if (word.length > 6) {
total += 8;
}
return total;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
// calculate score for each word and store it in a hash table, push it to an array
// calculate the highest score
let scoredWords = [];
let maxScore = 0;

for (const word of words) {
let wordHash = {};
wordHash.word = word;
wordHash.score = scoreWord(word);
scoredWords.push(wordHash);
if (wordHash.score > maxScore) {
maxScore = wordHash.score;
}
}

// tiebreakers based on word length
let minLength = 10;
let bestWord = "";
for (const wordObj of scoredWords) {
if (wordObj.score === maxScore) {
if (wordObj.word.length === 10) {
bestWord = wordObj.word;
{
break;
}
Comment on lines +110 to +112

Choose a reason for hiding this comment

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

Since we know this is definitely the best word, we can just do return wordObj from here and avoid the logic at the bottom looking for the object in the array again.

} else if (wordObj.word.length < minLength) {
bestWord = wordObj.word;

Choose a reason for hiding this comment

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

Instead of keeping track of just the best word, we can keep track of the whole wordObj that's the current best. This will allow us to avoid the logic later of trying to find the object again.

minLength = wordObj.word.length;
}
}
}

// iterate over array of word objects and return the best one
for (const wordObj of scoredWords) {
if (wordObj.word === bestWord) {
return wordObj;
}
}
};
8 changes: 5 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ describe("Adagrams", () => {
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expectScores({
"": 0,
});
Comment on lines +123 to +125

Choose a reason for hiding this comment

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

Excellent!

});

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +135,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 +147,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);

Choose a reason for hiding this comment

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

Excellent!

});

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