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

Leaves - Dominique #37

Open
wants to merge 4 commits into
base: master
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
131 changes: 129 additions & 2 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,135 @@
const Adagrams = {

drawLetters() {
// Implement this method for wave 1
const rawTiles = {

Choose a reason for hiding this comment

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

Style wise - I recommend putting the object outside of the drawLetters() function (but inside the module).

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 keys = Object.keys(rawTiles);

const letterPool = [];

for(const key in rawTiles) {

Choose a reason for hiding this comment

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

I really like that you store the letter frequencies and then build the pool - this is much easier to read than a giant array full of 9 'A's, 2 'B's, etc.

let i = 0

while (i <= rawTiles[key]) {
letterPool.push(key);
i += 1
};
};

const hand = [];

let i = 0;

while ( i <= 9) {

Choose a reason for hiding this comment

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

It looks like you could get the same letter twice because there's nothing keeping the random number from being repeated. Consider how you could address this.

hand.push(letterPool[Math.floor(Math.random()*letterPool.length)]);
i += 1
}
return hand;
},

usesAvailableLetters(input, lettersInHand) {
let lettersAvailable = true;

const checkForLetter = function(letter) {
if (lettersInHand.includes(letter)) {
let index = lettersInHand.indexOf(letter);
lettersInHand.splice(index, 1);

} else {
// console.log(`Letter is ${letter}`);
// console.log(`lettersInHand is ${lettersInHand}`);
lettersAvailable = false;
}
}

input.split('').forEach(checkForLetter);

Choose a reason for hiding this comment

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

Consider whether there's a different type of for loop that you could use that would allow you to break out of the loop as soon as you encounter false.

return lettersAvailable;
},
};



scoreWord(word) {
const scoreChart = {

Choose a reason for hiding this comment

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

Style wise - I recommend putting the object scoreChart outside of the scoreWod() function (but inside the module).

Choose a reason for hiding this comment

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

Style note: You should create this object the same way you did for rawTiles:

const scoreChart = { A: 1, B: 3 ... }

"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 wordScore = 0;
const wordDowncase = word.toLowerCase();

wordDowncase.split('').forEach( function(letter) {
if (scoreChart[letter]) {
wordScore += scoreChart[letter];
};
});

if (wordDowncase.length >= 7) {
wordScore += 8;
}

return wordScore


// Make a score chart hash.
// Create a variable called wordScore
// if given empty string, return a score of 0
// Iterate through the word and if hash[letter], assign the value of that letter to the
// variable wordScore
// additional points for length of the word can be added on later.
// calculate the length of the word and assign additional points if necessary
},

}

// Do not remove this line or your tests will break!
export default Adagrams;
4 changes: 2 additions & 2 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Adagrams', () => {
});
});

describe.skip('highestScoreFrom', () => {
xdescribe('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: Adagrams.scoreWord('XXXX') };
Expand All @@ -99,7 +99,7 @@ describe('Adagrams', () => {
expect(Adagrams.highestScoreFrom(words)).toEqual(correct);
});

describe('in case of tied score', () => {
xdescribe.skip('in case of tied score', () => {
const expectTie = (words) => {
const scores = words.map(word => Adagrams.scoreWord(word));
const highScore = scores.reduce((h, s) => h < s ? s : h, 0);
Expand Down