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

Kate - Lion - js-adagrams #44

Open
wants to merge 12 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
115 changes: 111 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,122 @@
const letterDist = {

Choose a reason for hiding this comment

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

Excellent!

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,
};

export const drawLetters = () => {

Choose a reason for hiding this comment

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

Excellent!

// Implement this method for wave 1
let letterPool = [];
let hand = [];

// create letter pool from letter distribution
for (let letter in letterDist) {
for (let i = 0; i < letterDist[letter]; i++) {
letterPool.push(letter);
}
}

// for (let i = 0; i < 10; i++) {
while (hand.length < 10) {
let randomIndex = parseInt(Math.random() * letterPool.length);
let drawnLetter = letterPool[randomIndex];
hand.push(drawnLetter);
letterPool.splice(randomIndex, 1);
}

return hand;
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
for (let letter of input) {
if (lettersInHand.includes(letter)) {
let i = lettersInHand.indexOf(letter);
lettersInHand.splice(i, 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 lettersInHand array. This function should just be checking to make sure all of the letters in input is in lettersInHand. If the user is losing letters every time we do this check, there will be no hand left soon.

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

export const scoreWord = (word) => {
// Implement this method for wave 3
// create dictionary with score for each letter
let letterScore = {};
const buildScoreDict = (letters, score) => {
for (const letter of letters) {
letterScore[letter] = score;
}
};

buildScoreDict(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 1);
buildScoreDict(["D", "G"], 2);
buildScoreDict(["B", "C", "M", "P"], 3);
buildScoreDict(["F", "H", "V", "W", "Y"], 4);
buildScoreDict(["K"], 5);
buildScoreDict(["J", "X"], 8);
buildScoreDict(["Q", "Z"], 10);
Comment on lines +66 to +79

Choose a reason for hiding this comment

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

This can be done by just creating the object like you do with the letterDist.


// calculate score
word = word.toUpperCase();

let points = 0;
if (word.length === 0) {
return points;
} else if (word.length > 6) {
points += 8;
}
for (let letter of word) {
points += letterScore[letter];
}
return points;
Comment on lines +82 to +93

Choose a reason for hiding this comment

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

Excellent!

};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
let max = 0;
let highestScoreWord = "";
for (let word of words) {

Choose a reason for hiding this comment

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

Instead of calling scoreWord multiple times, it would be more efficient to call it once at the beginning of the for loop and store it in a variable.

if (scoreWord(word) > max) {
max = scoreWord(word);
highestScoreWord = word;
} else if (scoreWord(word) === max) {
if (word.length != 10 && highestScoreWord.length != 10) {
if (word.length < highestScoreWord.length) {
highestScoreWord = word;
}
} else if (word.length === 10 && highestScoreWord.length != 10) {
highestScoreWord = word;
} else if (word.length != 10 && highestScoreWord.length === 10) {
highestScoreWord = highestScoreWord;
} else if (word.length === highestScoreWord) {
highestScoreWord = highestScoreWord;
Comment on lines +110 to +113

Choose a reason for hiding this comment

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

Since the highestScoreWord is already the highestScoreWord, these else if statements can be removed.

}
}
}
const output = {
word: highestScoreWord,
score: max,
};
return output;
};
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
Loading