Skip to content

Instantly share code, notes, and snippets.

@redrambles
Last active November 7, 2023 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redrambles/677cf1f3664507e03103a5a026620a8b to your computer and use it in GitHub Desktop.
Save redrambles/677cf1f3664507e03103a5a026620a8b to your computer and use it in GitHub Desktop.
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
};
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