diff --git a/Dockerfile b/Dockerfile index a5c6677d..f3b45d47 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,5 +12,5 @@ WORKDIR /app ARG SUBMISSION_SUBFOLDER ADD $SUBMISSION_SUBFOLDER /app -RUN yarn add +RUN yarn install RUN chmod +x test.sh diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..1ca0d264 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,15 +1,126 @@ +let letterPool = new Map() +letterPool.set("A", 9); +letterPool.set("B", 2); +letterPool.set("C", 2); +letterPool.set("D", 4); +letterPool.set("E", 12); +letterPool.set("F", 2); +letterPool.set("G", 3); +letterPool.set("H", 2); +letterPool.set("I", 9); +letterPool.set("J", 1); +letterPool.set("K", 1); +letterPool.set("L", 4); +letterPool.set("M", 2); +letterPool.set("N", 6); +letterPool.set("O", 8); +letterPool.set("P", 2); +letterPool.set("Q", 1); +letterPool.set("R", 6); +letterPool.set("S", 4); +letterPool.set("T", 6); +letterPool.set("U", 4); +letterPool.set("V", 2); +letterPool.set("W", 2); +letterPool.set("X", 1); +letterPool.set("Y", 2); +letterPool.set("Z", 1); + + export const drawLetters = () => { - // Implement this method for wave 1 -}; + const letterPoolCopy = new Map(JSON.parse(JSON.stringify(Array.from(letterPool)))); + const keys = Array.from(letterPoolCopy.keys()) + let lettersDraw = []; + let count = 10; + + while(count > 0){ + //Find the random index + let index = Math.floor(Math.random() * keys.length); + let indexCounter = 0; + + for (let key of letterPoolCopy.keys()) { + if (indexCounter === index) { + // If the letter already exceed it's avaliable draws, continue to do the next random draw + if(letterPoolCopy.get(key) > 0){ + count--; + lettersDraw.push(key); + letterPoolCopy.set(key, letterPoolCopy.get(key)-1); + } + break; + } + indexCounter++; + } + } + return lettersDraw; +} export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 -}; + // Create a copy of the lettersInHand + let lettersInHandCopy = JSON.parse(JSON.stringify(lettersInHand)); + for(const letter of input){ + // Check if lettersInHand includes the letter + if(lettersInHandCopy.includes(letter)){ + // Find the index of the letter that needs to be removed + const removeIndex = lettersInHandCopy.indexOf(letter); + // Remove the letter from lettersInHand + lettersInHandCopy.splice(removeIndex, 1); + }else{ // If the letter in the input is not in lettersInHand, return false + return false; + } + } + return true; +} export const scoreWord = (word) => { - // Implement this method for wave 3 -}; + let score = 0 + word = word.toUpperCase() + + if(word.length >= 7 && word.length <= 10){ + score += 8 + } + + for(const ch of word){ + if(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"].includes(ch)){ + score += 1 + }else if(["D", "G"].includes(ch)){ + score += 2 + }else if(["B", "C", "M", "P"].includes(ch)){ + score += 3 + }else if(["F", "H", "V", "W", "Y"].includes(ch)){ + score += 4 + }else if(ch === "K"){ + score += 5 + }else if(["J", "X"].includes(ch)){ + score += 8 + }else{ + score += 10 + } + } + return score +} export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + let highestScoreWord = { + score: 0, + word: "" + } + + for(const word of words){ + const currScore = scoreWord(word); + if(currScore > highestScoreWord["score"]){ + highestScoreWord["word"] = word; + highestScoreWord["score"] = currScore; + }else if(currScore === highestScoreWord["score"]){ + if(highestScoreWord["word"].length === 10){ + continue; + } else if(word.length === 10){ + highestScoreWord["word"] = word; + continue; + }else if(word.length < highestScoreWord["word"].length){ + highestScoreWord["word"] = word; + } + } + } + + return highestScoreWord }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..094a1806 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,7 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - throw "Complete test"; + expectScores({}); }); it("adds an extra 8 points if word is 7 or more characters long", () => { @@ -133,7 +133,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 +145,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", () => { diff --git a/test/demo/model.test.js b/test/demo/model.test.js index 49bf9599..f877beef 100644 --- a/test/demo/model.test.js +++ b/test/demo/model.test.js @@ -1,7 +1,7 @@ import Model from 'demo/model'; import Adagrams from 'demo/adagrams'; -describe.skip('Game Model', () => { +describe ('Game Model', () => { const config = { players: [ 'Player A',