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

Cheetahs- Tasha C. #45

Open
wants to merge 15 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
143 changes: 133 additions & 10 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,138 @@
export const drawLetters = () => {
// Implement this method for wave 1
const letterPool = {

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 usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
const scoreChart = {

Choose a reason for hiding this comment

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

Excellent!

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

export const scoreWord = (word) => {
// Implement this method for wave 3
};
// ------------ Wave 05---------------------------
class Adagrams {
static drawLetters() {

Choose a reason for hiding this comment

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

Excellent!

let availableLetters = { ...letterPool };
const lettersInHand = [];

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
};
while (lettersInHand.length < 10) {
const letter = alphabet[Math.floor(Math.random() * alphabet.length)];

if (availableLetters[letter] > 0) {
availableLetters[letter]--;
lettersInHand.push(letter);
}
}
return lettersInHand;
}

static usesAvailableLetters(input, lettersInHand) {

Choose a reason for hiding this comment

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

Excellent!

const drawnDict = {};
lettersInHand.forEach((letter) => {
if (letter in drawnDict) {
drawnDict[letter]++;
} else {
drawnDict[letter] = 1;
}
});
const word = [...input.toUpperCase()];

for (const letter of word) {
if (drawnDict[letter] == 0 || !drawnDict[letter]) {
return false;
} else {
drawnDict[letter]--;
}
}

return true;
}

static scoreWord(word) {

Choose a reason for hiding this comment

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

Excellent!

const wordList = [...word.toUpperCase()];
let score = 0;
if (wordList.length >= 7) {
score += 8;
}

wordList.forEach((letter) => (score += scoreChart[letter]));

return score;
}

static highestScoreFrom(words) {

Choose a reason for hiding this comment

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

Excellent!

let highestScore = 0;
let winningWord = "";

words.forEach((word) => {
const score = Adagrams.scoreWord(word);

if (score > highestScore) {
highestScore = score;
winningWord = word;
} else if (score === highestScore) {
if (word.length === 10 && winningWord.length != 10) {
winningWord = word;
} else if (
word.length < winningWord.length &&
winningWord.length != 10
) {
winningWord = word;
}
}
});

const winner = { word: winningWord, score: highestScore };
return winner;
}
}

export default Adagrams;
63 changes: 31 additions & 32 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
drawLetters,
usesAvailableLetters,
scoreWord,
highestScoreFrom,
} from "adagrams";

import Adagrams from "adagrams";
const LETTER_POOL = {
A: 9,
B: 2,
Expand Down Expand Up @@ -36,14 +30,16 @@ const LETTER_POOL = {

describe("Adagrams", () => {
describe("drawLetters", () => {
// const newAdagram = new Adagrams()

it("draws ten letters from the letter pool", () => {
const drawn = drawLetters();
const drawn = Adagrams.drawLetters();

expect(drawn).toHaveLength(10);
});

it("returns an array, and each item is a single-letter string", () => {
const drawn = drawLetters();
const drawn = Adagrams.drawLetters();

expect(Array.isArray(drawn)).toBe(true);
drawn.forEach((l) => {
Expand All @@ -53,7 +49,7 @@ describe("Adagrams", () => {

it("does not draw a letter too many times", () => {
for (let i = 0; i < 1000; i++) {
const drawn = drawLetters();
const drawn = Adagrams.drawLetters();
const letter_freq = {};
for (let letter of drawn) {
if (letter in letter_freq) {
Expand All @@ -73,33 +69,34 @@ describe("Adagrams", () => {
describe("usesAvailableLetters", () => {
it("returns true if the submitted letters are valid against the drawn letters", () => {
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
const word = "DOG";

const isValid = usesAvailableLetters(word, drawn);
const word = "DOG";
const isValid = Adagrams.usesAvailableLetters(word, drawn);
expect(isValid).toBe(true);
});

it("returns false when word contains letters not in the drawn letters", () => {
const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"];
const word = "DOG";

const isValid = usesAvailableLetters(word, drawn);
const isValid = Adagrams.usesAvailableLetters(word, drawn);
console.log(isValid);
expect(isValid).toBe(false);
});

it("returns false when word contains repeated letters more than in the drawn letters", () => {
const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"];
const word = "GOOD";

const isValid = usesAvailableLetters(word, drawn);
const isValid = Adagrams.usesAvailableLetters(word, drawn);
expect(isValid).toBe(false);
});
});

describe("scoreWord", () => {
const expectScores = (wordScores) => {
Object.entries(wordScores).forEach(([word, score]) => {
expect(scoreWord(word)).toBe(score);
expect(Adagrams.scoreWord(word)).toBe(score);
});
};

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

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

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,24 +132,24 @@ 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") };
const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") };

expect(highestScoreFrom(words)).toEqual(correct);
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
});

it("accurately finds best scoring word even if not sorted", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };
const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") };

throw "Complete test by adding an assertion";
expect(Adagrams.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", () => {
const expectTie = (words) => {
const scores = words.map((word) => scoreWord(word));
const scores = words.map((word) => Adagrams.scoreWord(word));
const highScore = scores.reduce((h, s) => (h < s ? s : h), 0);
const tiedWords = scores.filter((s) => s == highScore);

Expand All @@ -162,37 +161,37 @@ describe("Adagrams", () => {
const words = ["AAAAAAAAAA", "BBBBBB"];
const correct = {
word: "AAAAAAAAAA",
score: scoreWord("AAAAAAAAAA"),
score: Adagrams.scoreWord("AAAAAAAAAA"),
};
expectTie(words);

expect(highestScoreFrom(words)).toEqual(correct);
expect(highestScoreFrom(words.reverse())).toEqual(correct);
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct);
});

it("selects the word with fewer letters when neither are 10 letters", () => {
const words = ["MMMM", "WWW"];
const correct = { word: "WWW", score: scoreWord("WWW") };
const correct = { word: "WWW", score: Adagrams.scoreWord("WWW") };
expectTie(words);

expect(highestScoreFrom(words)).toEqual(correct);
expect(highestScoreFrom(words.reverse())).toEqual(correct);
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct);
});

it("selects the first word when both have same length", () => {
const words = ["AAAAAAAAAA", "EEEEEEEEEE"];
const first = {
word: "AAAAAAAAAA",
score: scoreWord("AAAAAAAAAA"),
score: Adagrams.scoreWord("AAAAAAAAAA"),
};
const second = {
word: "EEEEEEEEEE",
score: scoreWord("EEEEEEEEEE"),
score: Adagrams.scoreWord("EEEEEEEEEE"),
};
expectTie(words);

expect(highestScoreFrom(words)).toEqual(first);
expect(highestScoreFrom(words.reverse())).toEqual(second);
expect(Adagrams.highestScoreFrom(words)).toEqual(first);
expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(second);
});
});
});
Expand Down
Loading