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 .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
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
112 changes: 61 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"regenerator-runtime": "^0.12.1"
},
"dependencies": {
"axios": "^0.19.0",
"core-js": "^2.6.6",
"vorpal": "^1.11.4"
}
Expand Down
79 changes: 77 additions & 2 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,83 @@
const Adagrams = {
drawLetters() {
// Implement this method for wave 1
const letterPool = ('A'.repeat(10) + 'B'.repeat(2) + 'C'.repeat(2) + 'D'.repeat(4) + 'E'.repeat(12) + 'F'.repeat(2) + 'G'.repeat(3) + 'H'.repeat(2) + 'I'.repeat(9) + 'J' + 'K' + 'L'.repeat(4) + 'M'.repeat(2) + 'N'.repeat(6) + 'O'.repeat(8) + 'P'.repeat(2) + 'Q' + 'R'.repeat(6) + 'S'.repeat(4) + 'T'.repeat(6) + 'U'.repeat(4) + 'V'.repeat(2) + 'W'.repeat(2) + 'X' + 'Y'.repeat(2) + 'Z').split('');

Choose a reason for hiding this comment

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

Style note: Consider giving each letters it's own line.


const letterHandIndices = [];
do {

Choose a reason for hiding this comment

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

Love the use of the do-while! :)

let newIndex = Math.floor(Math.random() * letterPool.length);
if (!letterHandIndices.includes(newIndex)) {
letterHandIndices.push(newIndex) }
}
while (letterHandIndices.length < 10);

const letterHand = [];

letterHandIndices.forEach( function(index) {

Choose a reason for hiding this comment

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

consider using an arrow function for the anonymous function in the forEach loop

letterHand.push(letterPool[index])
});
return letterHand
},

usesAvailableLetters(input, lettersInHand) {
const lettersInHandCopy = Array.from(lettersInHand);
const inputArray = input.split('');
let validHand = true
inputArray.forEach( function(char) {

Choose a reason for hiding this comment

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

consider refactoring to a different type of loop so that you could return false as soon as it's encountered.

if (lettersInHandCopy.includes(char)) {
lettersInHandCopy.splice(lettersInHandCopy.indexOf(char), 1)
} else { validHand = false;
}
});
return validHand
},

highestScoreFrom(words) {

Choose a reason for hiding this comment

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

style note: even though it's not required, placing score(word) above highestScoreFrom(words) is best practice since the later function uses the former.

let wordStats = words.map(x => ( { word: x, score: this.scoreWord(x) } ));

wordStats.sort(function (a, b) {
return b.score - a.score;
});

let bestWord = wordStats[0];

for (const element of wordStats) {
if (element.score === bestWord.score && element.word.length === 10) {
return element
} else if (element.score === bestWord.score && element.word.length < bestWord.word.length) {
bestWord = element
}
}
return bestWord
},
};

scoreWord(word) {
const scores = {
"AEIOULNRST": 1,
"DG": 2,
"BCMP": 3,
"FHVWY": 4,
"K": 5,
"JX": 8,
"QZ": 10
}
const wordArray = word.toUpperCase().split('');
let wordScore = 0;

wordArray.forEach( function(char) {
for (const key in scores) {
if (key.includes(char)) {
wordScore += scores[key];
}
}
});
if (wordArray.length > 6) {

Choose a reason for hiding this comment

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

&& wordArray.length < 11

wordScore += 8;
}

return wordScore
}
}

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