-
-
Save redrambles/677cf1f3664507e03103a5a026620a8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const wordList = ["apple", "cherry", "banana", "grape"]; | |
const letterValues = [...Array(26).keys()].reduce((scores, number) => { | |
scores[String.fromCharCode(97 + number)] = number + 1; | |
return scores; | |
}, {}); // {a: 1, b: 2, c: 3 ...} | |
const calculateWordScore = (word) => { | |
return word | |
.toLowerCase() | |
.split("") | |
.reduce((total, letter) => { | |
total += letterValues[letter]; | |
return total; | |
}, 0); | |
}; | |
const wordScores = wordList.reduce((totalsArray, word) => { | |
totalsArray.push(calculateWordScore(word)); | |
return totalsArray; | |
}, []); | |
const highestScoringWord = (wordList, wordScores) => { | |
if (wordList.length === 0) return; | |
const wordIndex = wordScores.indexOf(Math.max(...wordScores)); | |
return wordList[wordIndex]; | |
}; | |
module.exports = { | |
wordList, | |
letterValues, | |
wordScores, | |
calculateWordScore, | |
highestScoringWord | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { wordList, letterValues, calculateWordScore, wordScores, highestScoringWord } = require("./highestScoringWord.js"); | |
describe("Word Scoring Test, let's gooooo", () => { | |
test("letterValues should have 26 items", () => { | |
expect(Object.keys(letterValues).length).toBe(26); | |
}); | |
test("calculateWordScore should calculate correctly for a single word", () => { | |
expect(calculateWordScore("Cassidy")).toBe(80); | |
}); | |
test("wordScores should produce an array with the same amount of items as wordList", () => { | |
expect(wordScores.length).toEqual(wordList.length); | |
}); | |
test("highestScoringWord should return... the highest scoring word", () => { | |
expect(highestScoringWord(wordList, wordScores)).toBe("cherry"); | |
}); | |
test("highestScoringWord should handle an empty word list with poise and grace", () => { | |
expect(highestScoringWord([], wordScores)).toBe(undefined); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment