Skip to content
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 @@
export const drawLetters = () => {
// Implement this method for wave 1
const letterPool = [
...Array(9).fill('A'),
...Array(2).fill('B'),
...Array(2).fill('C'),
...Array(4).fill('D'),
...Array(12).fill('E'),
...Array(2).fill('F'),
...Array(3).fill('G'),
...Array(2).fill('H'),
...Array(9).fill('I'),
'J',
'K',
...Array(4).fill('L'),
...Array(2).fill('M'),
Comment on lines +2 to +15

Choose a reason for hiding this comment

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

Hi Hannah! Great job completing JS Adagrams! Congrats on finishing your first JavaScript project 🎉 Excellent choice with this data structure. There's a lot of repeated code here, how can we make this program more DRY?

...Array(6).fill('N'),
...Array(8).fill('O'),
...Array(2).fill('P'),
'Q',
...Array(6).fill('R'),
...Array(4).fill('S'),
...Array(6).fill('T'),
...Array(4).fill('U'),
...Array(2).fill('V'),
...Array(2).fill('W'),
'X',
...Array(2).fill('Y'),
'Z',
];
const hand = [];

while (hand.length < 10) {
const randomIndex = Math.floor(Math.random() * letterPool.length);
const letter = letterPool[randomIndex];

if (hand.filter((l) => l === letter).length < letterPool.filter((l) => l === letter).length) {
hand.push(letter);
}
Comment on lines +32 to +38

Choose a reason for hiding this comment

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

Nice use of filter() ⭐️

}

Comment on lines +32 to +40

Choose a reason for hiding this comment

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

Nice job translating this logic 👍🏾

return hand;
};

// check for input validity - can given letters form word
export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
const handCopy = [...lettersInHand];

for (const letter of input) {
const index = handCopy.indexOf(letter);

if (index === -1) {
return false;
}

handCopy.splice(index, 1);
Comment on lines +46 to +55

Choose a reason for hiding this comment

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

Nice job translating this logic 👍🏾

}
Comment on lines +48 to +56

Choose a reason for hiding this comment

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

Great work!


return true;
};

// calculate score for a word
export const scoreWord = (word) => {
// Implement this method for wave 3
if (word === "") {
return 0;
}

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

let score = 0;

for (const letter of word.toUpperCase()) {
score += letterScores[letter];
}

if (word.length >= 7 && word.length <= 10) {
score += 8;
}
Comment on lines +67 to +85

Choose a reason for hiding this comment

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

Great work getting comfortable with JavaScript's syntax. It looks like you're getting use to looping over data and adding conditional logic ✅


return score;
};


// find word w/ highest score
export const highestScoreFrom = (words) => {
// Implement this method for wave 4
let highestScore = 0;
let highestScoringWords = [];

for (const word of words) {
const score = scoreWord(word);

if (score > highestScore) {
highestScore = score;
highestScoringWords = [{ word, score }];
} else if (score === highestScore) {
highestScoringWords.push({ word, score });
Comment on lines +101 to +103

Choose a reason for hiding this comment

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

Great work using this object to implement this solution ✅

}
}

if (highestScoringWords.length === 1) {
return highestScoringWords[0];
} else {
let winningWord = highestScoringWords[0];

for (const wordData of highestScoringWords) {
if (wordData.word.length === 10) {
return wordData;
} else if (wordData.word.length < winningWord.word.length && winningWord.word.length !== 10) {
winningWord = wordData;
}
}

return winningWord;
}
};
20 changes: 16 additions & 4 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ describe("Adagrams", () => {
});

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

const result = scoreWord(word);

expect(result).toBe(correctScore);
});

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +138,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,9 +150,11 @@ describe("Adagrams", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };

throw "Complete test by adding an assertion";
});
const result = highestScoreFrom(words);

expect(result).toEqual(correct);
});

describe("in case of tied score", () => {
const expectTie = (words) => {
const scores = words.map((word) => scoreWord(word));
Expand Down Expand Up @@ -197,3 +204,8 @@ describe("Adagrams", () => {
});
});
});





2 changes: 1 addition & 1 deletion test/demo/model.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Model from 'demo/model';
import Adagrams from 'demo/adagrams';

describe.skip('Game Model', () => {
describe('Game Model', () => {
const config = {
players: [
'Player A',
Expand Down