From 9cadc3227b49bc6900275d78106e939e2a6d1081 Mon Sep 17 00:00:00 2001 From: Morgan Buntin Date: Mon, 6 Jun 2022 08:08:25 -0400 Subject: [PATCH 1/5] passed wave1 tests --- src/adagrams.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/adagrams.js b/src/adagrams.js index ebc2839c..030a1260 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,5 +1,58 @@ +const letterFreqAndScore = { + A: [9, 1], + B: [2, 3], + C: [2, 3], + D: [4, 2], + E: [12, 1], + F: [2, 4], + G: [3, 2], + H: [2, 4], + I: [9, 1], + J: [1, 8], + K: [1, 5], + L: [4, 1], + M: [2, 3], + N: [6, 1], + O: [8, 1], + P: [2, 3], + Q: [1, 10], + R: [6, 1], + S: [4, 1], + T: [6, 1], + U: [4, 1], + V: [2, 4], + W: [2, 4], + X: [1, 8], + Y: [2, 4], + Z: [1, 10], +}; + export const drawLetters = () => { // Implement this method for wave 1 + let letterList = []; + let returnedLetters = []; + + for (const [letter, number] of Object.entries(letterFreqAndScore)) { + for (let i = 0; i < number[0]; i++) { + letterList.push(letter); + } + } + + while (returnedLetters.length < 10) { + let currentLetter = + letterList[Math.floor(Math.random() * letterList.length)]; + returnedLetters.push(currentLetter); + for (let i = 0; i < letterList.length; i++) { + if (letterList[i] === currentLetter) { + letterList.splice(i, 1); + { + break; + } + } + } + } + + return returnedLetters; }; export const usesAvailableLetters = (input, lettersInHand) => { From f3c830734fdbdff3948e2b748ee14ff84d00254d Mon Sep 17 00:00:00 2001 From: Morgan Buntin Date: Mon, 6 Jun 2022 08:08:59 -0400 Subject: [PATCH 2/5] no vscode config files --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f6851a87..86f5fb02 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules package.json.lock .DS_Store -coverage \ No newline at end of file +coverage +.vscode \ No newline at end of file From 4cc990ab7638ca217b81225db29f733cccf24ad9 Mon Sep 17 00:00:00 2001 From: Morgan Buntin Date: Mon, 6 Jun 2022 09:41:10 -0400 Subject: [PATCH 3/5] passed wave2 tests --- src/adagrams.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/adagrams.js b/src/adagrams.js index 030a1260..8e1b4032 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -57,6 +57,25 @@ export const drawLetters = () => { export const usesAvailableLetters = (input, lettersInHand) => { // Implement this method for wave 2 + let letterDict = {}; + + for (const letter of lettersInHand) { + if (letterDict.hasOwnProperty(letter.toUpperCase())) { + letterDict[letter] += 1; + } else { + letterDict[letter] = 1; + } + } + + for (const letter of input) { + if (letterDict[letter] > 0) { + letterDict[letter] -= 1; + } else { + return false; + } + } + + return true; }; export const scoreWord = (word) => { From c4513e16f4247ebd08ff1ad235816e76bee5a827 Mon Sep 17 00:00:00 2001 From: Morgan Buntin Date: Mon, 6 Jun 2022 09:53:18 -0400 Subject: [PATCH 4/5] wrote empty string test, passes wave3 tests --- src/adagrams.js | 10 ++++++++++ test/adagrams.test.js | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/adagrams.js b/src/adagrams.js index 8e1b4032..6a63310d 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -80,6 +80,16 @@ export const usesAvailableLetters = (input, lettersInHand) => { export const scoreWord = (word) => { // Implement this method for wave 3 + let score = 0; + + for (const letter of word) { + score += letterFreqAndScore[letter.toUpperCase()][1]; + } + if (word.length > 6) { + score += 8; + } + + return score; }; export const highestScoreFrom = (words) => { diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..233b1759 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,9 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - throw "Complete test"; + expectScores({ + "": 0, + }); }); it("adds an extra 8 points if word is 7 or more characters long", () => { From 31e5ce9f8e2941e54245c07477c5d51df80a86bc Mon Sep 17 00:00:00 2001 From: Morgan Buntin Date: Mon, 6 Jun 2022 10:39:03 -0400 Subject: [PATCH 5/5] tests adjusted, code passes wave4 and all tests --- src/adagrams.js | 42 ++++++++++++++++++++++++++++++++++++++++++ test/adagrams.test.js | 4 ++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 6a63310d..a0aa856a 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -94,4 +94,46 @@ export const scoreWord = (word) => { export const highestScoreFrom = (words) => { // Implement this method for wave 1 + let scores = []; + let lowest = []; + const result = { + word: null, + score: null, + }; + + for (const word of words) { + let currentWordScore = scoreWord(word); + + if (scores.length === 0) { + scores.push([word, currentWordScore]); + } else if (currentWordScore > scores[0][1]) { + scores = [[word, currentWordScore]]; + } else if (currentWordScore === scores[0][1]) { + scores.push([word, currentWordScore]); + } + } + + if (scores.length === 1) { + result["word"] = scores[0][0]; + result["score"] = scores[0][1]; + return result; + } + + for (const [word, score] of scores) { + let wordLength = word.length; + + if (wordLength === 10) { + result["word"] = word; + result["score"] = score; + return result; + } else if (lowest.length === 0) { + lowest = [word, score, wordLength]; + } else if (wordLength < lowest[2]) { + lowest = [word, score, wordLength]; + } + } + + result["word"] = lowest[0]; + result["score"] = lowest[1]; + return result; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 233b1759..fc35fcf9 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -135,7 +135,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: scoreWord("XXXX") }; @@ -147,7 +147,7 @@ describe("Adagrams", () => { const words = ["XXX", "XXXX", "X", "XX"]; const correct = { word: "XXXX", score: scoreWord("XXXX") }; - throw "Complete test by adding an assertion"; + expect(highestScoreFrom(words)).toEqual(correct); }); describe("in case of tied score", () => {