diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..5dc84017 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,15 +1,131 @@ export const drawLetters = () => { - // Implement this method for wave 1 + // initialize object to hold letter frequency table + const letterFreqs = { + 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, + }; + + // fill letterPool array with all the letters + const letterPool = []; + + for (const [key, value] of Object.entries(letterFreqs)) { + for (let i = 1; i < value + 1; i++) { + letterPool.push(key); + } + } + + // draw letters into hand array + let hand = []; + + for (let i = 0; i < 10; i++) { + let selector = Math.random() * letterPool.length; + + let letter = letterPool.splice(selector, 1)[0]; + + hand.push(letter); + } + + return hand; }; export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 + let handCopy = lettersInHand; + + for (let i = 0; i < input.length; i++) { + let letter = input[i]; + + if (handCopy.includes(letter)) { + let index = handCopy.indexOf(letter); + handCopy.splice(index, 1); + } else { + return false; + } + } + + return true; }; export const scoreWord = (word) => { // Implement this method for wave 3 + if (!word) { + return 0; + } + + const scoreChart = { + 1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + 2: ["D", "G"], + 3: ["B", "C", "M", "P"], + 4: ["F", "H", "V", "W", "Y"], + 5: ["K"], + 8: ["J", "X"], + 10: ["Q", "Z"], + }; + + let score = 0; + + if (word.length >= 7) { + score += 8; + } + + for (let i = 0; i < word.length; i++) { + let letter = word[i]; + for (const [key, value] of Object.entries(scoreChart)) { + if (value.includes(letter.toUpperCase())) { + score += parseInt(key); + // console.log(score); + } + } + } + + return score; }; export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + let largestWord = ""; + let largestWordScore = 0; + + for (let i = 0; i < words.length; i++) { + let word = words[i]; + let wordScore = scoreWord(word); + if (wordScore > largestWordScore) { + largestWord = word; + largestWordScore = wordScore; + } else if (wordScore === largestWordScore) { + if (largestWord.length === 10) { + largestWord = largestWord; + } else if (word.length === 10) { + largestWord = word; + } else if (word.length < largestWord.length) { + largestWord = word; + } else if (largestWord.length < word.length) { + largestWord = largestWord; + } + } + } + let result = { word: largestWord, score: largestWordScore }; + return result; }; diff --git a/src/demo.js b/src/demo.js index 3f8a67f2..41c349c0 100644 --- a/src/demo.js +++ b/src/demo.js @@ -1,6 +1,6 @@ -import Model from 'demo/model'; -import View from 'demo/view'; -import Controller from 'demo/controller'; +import Model from "demo/model"; +import View from "demo/view"; +import Controller from "demo/controller"; // Initialize the controller I guess const game = new Controller(Model, View); diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..fc35fcf9 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", () => { @@ -133,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") }; @@ -145,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", () => {