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
172 changes: 170 additions & 2 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,176 @@
import { objectExpression } from "@babel/types";

const makeLetters = function() {
const alphabetObject = {
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 alphabetArray = [];
for (let key in alphabetObject) {
let i = alphabetObject[key];
while (i > 0) {
alphabetArray.push(key);
i -= 1;
}
}
return alphabetArray;
};

// adapted from Fisher-Yates Shuffle
const shuffle = function(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};

const scoreWord = function(word) {
const scoreboard = {
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,
};
let score = 0;
if (word.length >= 7) {
score += 8;
};
word = word.toUpperCase().split('');
word.forEach((letter) => {
if (Object.keys(scoreboard).includes(letter)) {
score += scoreboard[letter];
}
})
return score;
};

const Adagrams = {
makeLetters,
shuffle,

drawLetters() {
// Implement this method for wave 1

Choose a reason for hiding this comment

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

Here, you shuffle the entire array pool of letters... and then, you take the first ten. Consider refactoring this so that you randomly pick ten letters, instead of shuffling the entire array.

let shuffled = this.shuffle(this.makeLetters());
let lettersInHand = shuffled.slice(0, 10);
return lettersInHand;
},
};

usesAvailableLetters(input, lettersInHand) {
// put drawn letters into a hash-like object
let handCollection = {}
lettersInHand.forEach((letter) => {
if (handCollection[letter] == null) {
handCollection[letter] = 1;
}
else {
handCollection[letter] += 1;
}
})
// iterate through input word and check against hash-like object
let letterIndex = 0;
input = input.toUpperCase().split('')
while (letterIndex < input.length) {
if (Object.keys(handCollection).includes(input[letterIndex])) {
handCollection[input[letterIndex]] -= 1;
if (handCollection[input[letterIndex]] < 0) {
return false;
}
}
else if (!Object.keys(handCollection).includes(input[letterIndex])) {
return false;
}
letterIndex += 1;
}
return true
},

scoreWord,

highestScoreFrom(words) {
let wordCollection = [];

Choose a reason for hiding this comment

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

Consider using comments to outline your logic here. There's a lot going on, there may be an opportunity for a helper function.

let maxPoints = 0;
words.forEach((word) => {
let score = this.scoreWord(word);
wordCollection.push({word: word, score: score});
if (score > maxPoints) {
maxPoints = score;
};
});
let winners = []
wordCollection.forEach((object) => {
if (Object.values(object).includes(maxPoints)) {
winners.push(object);
}
})
if (winners.length === 1) {
return winners[0];
}
let wordLength = 10;
let shortest = null;
let index = 0;
while (index < winners.length) {
if ((winners[index].word).length === 10) {
return winners[index];
}
if ((winners[index].word).length < wordLength) {
wordLength = (winners[index].word).length;
shortest = (winners[index]);
}
index += 1;
}
return shortest;
}
}

// 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