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 babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const presets = [
safari: "11.1",
},
useBuiltIns: "usage",
corejs: 2,
},
],
];
Expand Down
62 changes: 61 additions & 1 deletion src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@
const Adagrams = {
drawLetters() {
// Implement this method for wave 1
const alphabet = 'AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ';

Choose a reason for hiding this comment

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

This data structure works, but it's not very DRY and would be tricky to change. For example, suppose I said you had the wrong number of "I"s - you would have to do a lot of counting to solve the problem.

Instead you might store a hash of letter frequencies like this

letterQuantities: {
  A: 9, B: 2, ...
}

const lettersInHand = [];

for (let i = 0; i < 10; i++) {

Choose a reason for hiding this comment

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

There's a small bug here. Consider how you could make sure you don't pick the same letter 10 times (however unlikely it is).

lettersInHand.push(alphabet.charAt(Math.floor(Math.random() * alphabet.length)));
}
return lettersInHand;
},

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

for (let i = 0; i < (input.length); i++) {
if (lettersInHand.includes(input[i]) === false) {
result = false;

Choose a reason for hiding this comment

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

since you used a traditional for loop you could simply return false here so you don't unnecessarily need to go through the whole loop.

} else {
lettersInHand.splice(lettersInHand.indexOf(input[i]), 1);
}
}
return result;
},

scoreWord(word) {
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,

Choose a reason for hiding this comment

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

style note: put each key-value pair on one line.

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 points = 0;
const wordCheck = word.toUpperCase();

if (wordCheck.length > 6) {
points += 8;
}

for (let i = 0; i < (wordCheck.length); i++) {
points += score[wordCheck[i]];
}
return points;
},

highestScoreFrom(words) {
let scores = [];

//Loops through the array of words and calling the method scoreWords to get the scores
for (let i = 0; i < (words.length); i++) {
scores.push(Adagrams.scoreWord(words[i]));
}

// The Math.max.apply(null, array) finds the max value on the array scores
// The function indexOf finds the value for the index with the max value
let correct = { word: words[scores.indexOf(Math.max.apply(null, scores))], score: Math.max.apply(null, scores) };

// Still need to work on the tie-breaking rules for Wave 4:

Choose a reason for hiding this comment

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

You've got a good start!

return correct;
},
};


// I ran each function bewlow and tested also on the console to troubleshoot failing tests etc.
// console.log(Adagrams.highestScoreFrom(["APPLE", "BLUE", "PIE"]))
// console.log(Adagrams.drawLetters());
// console.log(Adagrams.usesAvailableLetters('APPLE', ['A', 'P', 'P', 'L', 'E']));
// console.log(Adagrams.usesAvailableLetters('APPLES', ['A', 'P', 'P', 'L', 'E']));
// console.log(Adagrams.scoreWord('AVAILABLE'))

// Do not remove this line or your tests will break!
export default Adagrams;
2 changes: 1 addition & 1 deletion 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', () => {
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: Adagrams.scoreWord('XXXX') };
Expand Down