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
125 changes: 118 additions & 7 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
};
// Implement this method for wave 1
const letterPool =
{ '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
}
const listLetters = [];
while (listLetters.length != 10) {
let letters = Object.keys(letterPool);
let randomIndex = Math.floor(Math.random() * letters.length);
Comment on lines +33 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

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

This implementation has the same distribution issue that I mentioned in the Python version where there is an equal chance to pick any letter since we are choosing from the keys. Even though the tests pass, this doesn't meet what the function description is asking for, so I would like you to revisit this if you have time.

let randomLetter = letters[randomIndex];
let value = letterPool[randomLetter];
if (value > 0) {
letterPool[randomLetter] = value - 1;
listLetters.push(randomLetter);
}
}
return listLetters
}

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
};
// Implement this method for wave 2
input = input.toUpperCase();
const lettersCopy = lettersInHand.slice();
for (let letter of input) {
const index = lettersCopy.indexOf(letter);
if (index != -1) {
lettersCopy.splice(index, 1);
} else {
return false;
}
}
Comment on lines +49 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

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

This implementation has an O(n^2) time complexity, how could we use a frequency map to bring the time complexity down to O(n)?

return true;
}

export const scoreWord = (word) => {
// Implement this method for wave 3
// Implement this method for wave 3
const score = {
'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 totalScore = 0;
word = word.toUpperCase();
for (let i = 0; i < word.length; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could simplify the syntax inside the loop a little if we use a for-of loop to iterate directly over the letters in the parameter word:

for (const letter of word) {

let letter = word[i];
let value = score[letter] || 0;
totalScore += value;
}
if (word.length >= 7 && word.length <= 10) {
totalScore += 8;
}
return totalScore;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
};
// Implement this method for wave 4
if (words.length === 0) {
return ['', 0]
}
let highestWord = '';
let highestScore = 0;
for (let word of words) {
let wordScore = scoreWord(word);
if (wordScore > highestScore) {
highestScore = wordScore;
highestWord = word;
} else if (wordScore === highestScore) {
if (highestWord.length === 10) {
continue;
} else if (word.length === 10) {
highestWord = word;
} else if (word.length < highestWord.length) {
highestWord = word;
}
Comment on lines +118 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we combine these cases or do something else to reduce duplication of updating the highestWord variable?

}
}
return {'score': highestScore, 'word' : highestWord}
}
11 changes: 8 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ describe("Adagrams", () => {
});

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

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

throw "Complete test by adding an assertion";
//throw "Complete test by adding an assertion";

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

describe("in case of tied score", () => {
Expand Down
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