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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"core-js": "^3.8.0",
"package.json": "^2.0.1",
"vorpal": "^1.12.0"
}
}
134 changes: 130 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,141 @@
const LETTER_POOL = {
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 scoreChart = {

Choose a reason for hiding this comment

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

Great use of const! Since these constants at the top of the file are only accessed by one function each, it would be reasonable to place the objects inside the functions rather than as a constant. There are tradeoffs, the structure would clutter the function some, but it keeps the data as close as possible to where it's being used, and would mean that other functions who shouldn't need it couldn't access it.

As a constant outside of a function, I suggest using all caps naming: SCORE_CHART

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

export const drawLetters = () => {
// Implement this method for wave 1
let letterFrequency = {};
let aHandOfLetters = [];
while (aHandOfLetters.length < 10) {
let randomNum = Math.floor(Math.random() * 26);

Choose a reason for hiding this comment

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

This will give us an even chance of picking any single letter in the alphabet without going over the number of each tile we have. This is slightly different than what the README asks - we won't accurately represent the distribution of tiles because we pick a letter from 1-26, when the chances of picking some letters should be higher than others. How could we update the algorithm to account for this?

let randomLetter = String.fromCharCode(randomNum + 65);

if (
letterFrequency.hasOwnProperty(randomLetter) &&

Choose a reason for hiding this comment

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

Neat way to check if a key exists! We could also use in like

if (randomLetter in letterFrequency 
        && letterFrequency[randomLetter] < LETTER_POOL[randomLetter]) {
...
}

letterFrequency[randomLetter] < LETTER_POOL[randomLetter]
) {
aHandOfLetters.push(randomLetter);
letterFrequency[randomLetter] += 1;
} else if (!letterFrequency.hasOwnProperty(randomLetter)) {
aHandOfLetters.push(randomLetter);
letterFrequency[randomLetter] = 1;
}
}
return aHandOfLetters;
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
let found = 0;
for (const char of input) {
for (const i in lettersInHand) {
if (char === lettersInHand[i]) {
lettersInHand[i] = "";
found++;
break;
}
}
}
Comment on lines +82 to +90

Choose a reason for hiding this comment

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

Neat approach! If we needed to reduce the time complexity of our solution, another approach could be to build a frequency map of our hand then loop over the characters in the input, checking if the character is in our frequency map, and if it is, then check the value to see if there are still tiles left in our hand for that letter.

if (found === input.length) {
return true;
}
return false;
};

export const scoreWord = (word) => {
// Implement this method for wave 3
let totalPoints = 0;
for (const char of word) {

Choose a reason for hiding this comment

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

Nice use of a for...of loop.

totalPoints += scoreChart[char.toUpperCase()];
}
Comment on lines +99 to +101

Choose a reason for hiding this comment

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

This could have an unexpected result if the string word has characters that aren't in scoreChart. If we have an input like "ABC DEF" then totalPoints will hold NaN (Not a Number) after the loop. If we wanted to skip non-alphabetic characters or characters that aren't in scoreChart, how could we do that?

if (word.length >= 7) {
totalPoints += 8;
}
return totalPoints;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
let scoreWordsChart = {};
for (const word of words) {
let points = scoreWord(word);
scoreWordsChart[word] = points;
}

let inCaseTie = [];
let val = Object.values(scoreWordsChart);
const maxScore = Math.max(...val);

for (const word in scoreWordsChart) {
if (maxScore === scoreWordsChart[word]) {
inCaseTie.push(word);
}
}

if (inCaseTie.length === 1) {
let word = inCaseTie[0];
return { word: word, score: scoreWordsChart[word] };
}

let shortestWord = inCaseTie[0];

for (const word of inCaseTie) {
if (word.length === 10) {
return { word: word, score: scoreWordsChart[word] };
}
if (shortestWord.length > word.length) {
shortestWord = word;
}
}
return { word: shortestWord, score: scoreWordsChart[shortestWord] };
};
6 changes: 3 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Adagrams", () => {
});

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

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

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