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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.8.0",
"babel-plugin-module-resolver": "^3.2.0",
"eslint": "^8.29.0",
"eslint-plugin-jest": "^27.1.6",
"jest": "^24.8.0",
"regenerator-runtime": "^0.12.1"
},
Expand Down
217 changes: 217 additions & 0 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,232 @@
const TOTAL_TILES = 98
const STARTING_NUMBER = 1
const MAX_DRAW = 10

const WEIGHTED_LETTERS = {

Choose a reason for hiding this comment

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

Two notes here:

A way to keep your projects organized is to put large constant variables like this in a separate file and then import the data from that file into the places it needs to be used. Here's an example of how to do it.

The other thing is, what if you had a dictionary where the key was a letter and the value was the frequency of a letter {A: 9, B: 2, C: 2}. How would you iterate over this object to create a list of 98 letters to represent the frequency distribution?

1: 'A',
2: 'A',
3: 'A',
4: 'A',
5: 'A',
6: 'A',
7: 'A',
8: 'A',
9: 'A',
10: 'B',
11: 'B',
12: 'C',
13: 'C',
14: 'D',
15: 'D',
16: 'D',
17: 'D',
18: 'E',
19: 'E',
20: 'E',
21: 'E',
22: 'E',
23: 'E',
24: 'E',
25: 'E',
26: 'E',
27: 'E',
28: 'E',
29: 'E',
30: 'F',
31: 'F',
32: 'G',
33: 'G',
34: 'G',
35: 'H',
36: 'H',
37: 'I',
38: 'I',
39: 'I',
40: 'I',
41: 'I',
42: 'I',
43: 'I',
44: 'I',
45: 'I',
46: 'J',
47: 'K',
48: 'L',
49: 'L',
50: 'L',
51: 'L',
52: 'M',
53: 'M',
54: 'N',
55: 'N',
56: 'N',
57: 'N',
58: 'N',
59: 'N',
60: 'O',
61: 'O',
62: 'O',
63: 'O',
64: 'O',
65: 'O',
66: 'O',
67: 'O',
68: 'P',
69: 'P',
70: 'Q',
71: 'R',
72: 'R',
73: 'R',
74: 'R',
75: 'R',
76: 'R',
77: 'S',
78: 'S',
79: 'S',
80: 'S',
81: 'T',
82: 'T',
83: 'T',
84: 'T',
85: 'T',
86: 'T',
87: 'U',
88: 'U',
89: 'U',
90: 'U',
91: 'V',
92: 'V',
93: 'W',
94: 'W',
95: 'X',
96: 'Y',
97: 'Y',
98: 'Z'
};

const LETTER_VALUES = {

Choose a reason for hiding this comment

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

Nice job using const. We can use const for mutable objects and still add/remove from the object or array, we just can't reassign what LETTER_VALUES references

'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
}

// Helper functions //

const pickRandomNumber = () => Math.floor(Math.random() * TOTAL_TILES) + STARTING_NUMBER;

const calculateScore = (word) => {
let score = 0;
for (const letter of word) {
score = score + LETTER_VALUES[letter];
}
if (word.length > 6) {
score = score + 8
}
return score;
}


export const drawLetters = () => {
// Implement this method for wave 1
const keysArray = [];
const lettersInHand = [];

for (let i = 0; keysArray.length < MAX_DRAW; i++) {
const key = pickRandomNumber();
if (!keysArray.includes(key)) {
keysArray.push(key);
}
}

for (const key of keysArray) {
let tileLetter = WEIGHTED_LETTERS[key];
lettersInHand.push(tileLetter);
}

Comment on lines +156 to +167

Choose a reason for hiding this comment

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

Could these two for-loops be combined into one loop? How would your logic need to change so you could get a random key and push the associated letter into lettersInHand?

return lettersInHand;
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
const lettersInHandObject = {};

for (const letter of lettersInHand) {
if (letter in lettersInHandObject) {
lettersInHandObject[letter] += 1;
} else {
lettersInHandObject[letter] = 1;
}
}

for (const letter of input) {
if (letter in lettersInHandObject) {
lettersInHandObject[letter] -= 1;
if (lettersInHandObject[letter] < 0) {
return false;
}
} else {
return false;
}
Comment on lines +189 to +191

Choose a reason for hiding this comment

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

What scenario is this else block supposed to handle? Do we need it?

}
return true;

Choose a reason for hiding this comment

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

Another approach for checking if the input uses available letters is to loop through input with a for loop and check that the letter at each index is included in lettersInHand

 for (let i = 0; i < input.length; i++) {
    let letter = input[i];
    if (lettersInHand.includes(letter)) {
      let letterIndex = lettersInHand.indexOf(letter);
      lettersInHand.splice(letterIndex, 1);
    } else {
      return false;
    }
  }
  return true;
};

};

export const scoreWord = (word) => {
// Implement this method for wave 3
const wordUpper = word.toUpperCase();
const score = calculateScore(wordUpper);

return score;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
const winningWord = {
word: '',
score: 0
}

for (const word of words) {
const wordScore = calculateScore(word);
if (wordScore > winningWord.score) {
winningWord.word = word;
winningWord.score = wordScore;
} else if (wordScore === winningWord.score) {
if (word.length === 10 || winningWord.word.length === 10) {
if (winningWord.word.length === 10) {
continue;
} else {
winningWord.word = word;
winningWord.score = wordScore;
}
} else {
if (word.length < winningWord.word.length) {
winningWord.word = word;
}
}
}
}
return winningWord;
};
12 changes: 7 additions & 5 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ describe("Adagrams", () => {

it("does not draw a letter too many times", () => {
for (let i = 0; i < 1000; i++) {
const drawn = drawLetters();
const drawn = drawLetters(); //10 letter array
const letter_freq = {};
for (let letter of drawn) {
for (let letter of drawn) { //cycle through 10 letter array
if (letter in letter_freq) {
letter_freq[letter] += 1;
} else {
Expand Down Expand Up @@ -120,7 +120,9 @@ 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 +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);
});

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