From cfe1b8e1f59a4da2cd7c70d692cf37517eb1250c Mon Sep 17 00:00:00 2001 From: Tasha Date: Tue, 29 Nov 2022 21:07:35 -0800 Subject: [PATCH 01/14] Completed wave 1 drawLetters function --- src/adagrams.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..e2491c87 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,5 +1,48 @@ +const letterPool = { + 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, +}; +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + export const drawLetters = () => { // Implement this method for wave 1 + let letterPoolDict = letterPool; + let hand = []; + let i = 0; + + while (i < 10) { + let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; + if (letterPoolDict[letter] < 0) { + letterPoolDict[letter] -= 1; + hand.push(letter); + i++; + } + } + return hand; }; export const usesAvailableLetters = (input, lettersInHand) => { From bbf514e9e17b7ea43973d3efbf86b6e4b7a58f30 Mon Sep 17 00:00:00 2001 From: Tasha Date: Wed, 30 Nov 2022 09:45:16 -0800 Subject: [PATCH 02/14] Debugged infinite loop by making shallow copy of letterPool --- src/adagrams.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index e2491c87..1b44c7e1 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -29,14 +29,13 @@ const letterPool = { const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; export const drawLetters = () => { - // Implement this method for wave 1 - let letterPoolDict = letterPool; + let letterPoolDict = {...letterPool}; let hand = []; let i = 0; while (i < 10) { let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; - if (letterPoolDict[letter] < 0) { + if (letterPoolDict[letter] > 0) { letterPoolDict[letter] -= 1; hand.push(letter); i++; @@ -46,7 +45,8 @@ export const drawLetters = () => { }; export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 + // handDict = {} + // for letter_ }; export const scoreWord = (word) => { From 90870951282fa29392947eb9bd0b2b8c72618980 Mon Sep 17 00:00:00 2001 From: Tasha Date: Wed, 30 Nov 2022 13:40:46 -0800 Subject: [PATCH 03/14] Completed wave 2 usesAvailableLetters function --- src/adagrams.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 1b44c7e1..9b18a4c9 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -29,24 +29,39 @@ const letterPool = { const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; export const drawLetters = () => { - let letterPoolDict = {...letterPool}; + let letterPoolDict = { ...letterPool }; let hand = []; - let i = 0; - while (i < 10) { + while (hand.length < 10) { let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; if (letterPoolDict[letter] > 0) { letterPoolDict[letter] -= 1; hand.push(letter); - i++; } } return hand; }; export const usesAvailableLetters = (input, lettersInHand) => { - // handDict = {} - // for letter_ + let handDict = {}; + + for (let letter of lettersInHand) { + if (letter in handDict) { + handDict[letter] += 1; + } else { + handDict[letter] = 1; + } + } + let word = [...input]; + + for (let letter of word) { + if (handDict[letter] === 0 || !handDict[letter]) { + return false; + } else { + handDict[letter] -= 1; + } + } + return true; }; export const scoreWord = (word) => { From 81eead6e355ed95ecb729a363f6c619496b4a92d Mon Sep 17 00:00:00 2001 From: Tasha Date: Wed, 30 Nov 2022 14:18:59 -0800 Subject: [PATCH 04/14] completed wave 3 scoreWord function and completed test expect score zero if empty input --- src/adagrams.js | 62 ++++++++++++++++++++++++++++++++++--------- test/adagrams.test.js | 4 ++- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 9b18a4c9..81e41adb 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -26,46 +26,84 @@ const letterPool = { Y: 2, Z: 1, }; + +const scoreChart = { + A: 1, + B: 3, + C: 3, + D: 2, + E: 1, + F: 4, + G: 2, + H: 4, + I: 1, + J: 8, + K: 5, + L: 1, + M: 3, + N: 1, + O: 1, + P: 3, + Q: 10, + R: 1, + S: 1, + T: 1, + U: 1, + V: 4, + W: 4, + X: 8, + Y: 4, + Z: 10, +}; const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; export const drawLetters = () => { let letterPoolDict = { ...letterPool }; - let hand = []; + let lettersInHand = []; - while (hand.length < 10) { + while (lettersInHand.length < 10) { let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; if (letterPoolDict[letter] > 0) { letterPoolDict[letter] -= 1; - hand.push(letter); + lettersInHand.push(letter); } } - return hand; + return lettersInHand; }; export const usesAvailableLetters = (input, lettersInHand) => { - let handDict = {}; + let drawnDict = {}; for (let letter of lettersInHand) { - if (letter in handDict) { - handDict[letter] += 1; + if (letter in drawnDict) { + drawnDict[letter] += 1; } else { - handDict[letter] = 1; + drawnDict[letter] = 1; } } - let word = [...input]; + + let word = [...input.toUpperCase()]; for (let letter of word) { - if (handDict[letter] === 0 || !handDict[letter]) { + if (drawnDict[letter] == 0 || !drawnDict[letter]) { return false; } else { - handDict[letter] -= 1; + drawnDict[letter] -= 1; } } + return true; }; export const scoreWord = (word) => { - // Implement this method for wave 3 + const wordList = [...word.toUpperCase()]; + let score = 0; + if (wordList.length >= 7) { + score += 8; + } + + wordList.forEach((letter) => (score += scoreChart[letter])); + 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 2bf8702b7683a0ce9bea92fdc4897113baceb711 Mon Sep 17 00:00:00 2001 From: Tasha Date: Wed, 30 Nov 2022 14:31:59 -0800 Subject: [PATCH 05/14] refactored usesAvailableLetters to use forEach --- src/adagrams.js | 4 ++-- test/adagrams.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 81e41adb..cc7759e7 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -74,13 +74,13 @@ export const drawLetters = () => { export const usesAvailableLetters = (input, lettersInHand) => { let drawnDict = {}; - for (let letter of lettersInHand) { + lettersInHand.forEach((letter) => { if (letter in drawnDict) { drawnDict[letter] += 1; } else { drawnDict[letter] = 1; } - } + }); let word = [...input.toUpperCase()]; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 233b1759..ec427e78 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") }; From 0d7dc41fb64c09ca490326b507d0373bfa335cf1 Mon Sep 17 00:00:00 2001 From: Tasha Date: Wed, 30 Nov 2022 19:04:39 -0800 Subject: [PATCH 06/14] completed wave 4 and highestScoreFrom function, completed unit tests --- src/adagrams.js | 28 ++++++++++++++++++++++++---- test/adagrams.test.js | 2 +- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index cc7759e7..956955eb 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -58,13 +58,14 @@ const scoreChart = { const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; export const drawLetters = () => { - let letterPoolDict = { ...letterPool }; + let availableLetters = { ...letterPool }; let lettersInHand = []; while (lettersInHand.length < 10) { let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; - if (letterPoolDict[letter] > 0) { - letterPoolDict[letter] -= 1; + + if (availableLetters[letter] > 0) { + availableLetters[letter] -= 1; lettersInHand.push(letter); } } @@ -107,5 +108,24 @@ export const scoreWord = (word) => { }; export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + let highestScore = 0; + let winningWord = ""; + + words.forEach((word) => { + const score = scoreWord(word); + + if (score > highestScore) { + highestScore = score; + winningWord = word; + } else if (score === highestScore) { + if (word.length === 10 && winningWord.length != 10) { + winningWord = word; + } else if (word.length < winningWord.length && winningWord.length != 10) { + winningWord = word; + } + } + }); + + const winner = { word: `${winningWord}`, score: highestScore }; + return winner; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index ec427e78..fc35fcf9 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -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", () => { From 45765c2f919b8bf6039c2efb6b0ed94b9cae0ef4 Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 13:23:24 -0800 Subject: [PATCH 07/14] Changed some variable names from 'let' to 'const' --- src/adagrams.js | 18 +++--- test/demo/model.test.js | 118 +++++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 65 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 956955eb..4a271890 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -59,13 +59,13 @@ const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; export const drawLetters = () => { let availableLetters = { ...letterPool }; - let lettersInHand = []; + const lettersInHand = []; while (lettersInHand.length < 10) { - let letter = alphabet[Math.floor(Math.random() * alphabet.length)]; + const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; if (availableLetters[letter] > 0) { - availableLetters[letter] -= 1; + availableLetters[letter]--; lettersInHand.push(letter); } } @@ -73,23 +73,23 @@ export const drawLetters = () => { }; export const usesAvailableLetters = (input, lettersInHand) => { - let drawnDict = {}; + const drawnDict = {}; lettersInHand.forEach((letter) => { if (letter in drawnDict) { - drawnDict[letter] += 1; + drawnDict[letter]++; } else { drawnDict[letter] = 1; } }); - let word = [...input.toUpperCase()]; + const word = [...input.toUpperCase()]; - for (let letter of word) { + for (const letter of word) { if (drawnDict[letter] == 0 || !drawnDict[letter]) { return false; } else { - drawnDict[letter] -= 1; + drawnDict[letter]--; } } @@ -126,6 +126,6 @@ export const highestScoreFrom = (words) => { } }); - const winner = { word: `${winningWord}`, score: highestScore }; + const winner = { word: winningWord, score: highestScore }; return winner; }; diff --git a/test/demo/model.test.js b/test/demo/model.test.js index 49bf9599..9616ec8b 100644 --- a/test/demo/model.test.js +++ b/test/demo/model.test.js @@ -1,48 +1,45 @@ -import Model from 'demo/model'; -import Adagrams from 'demo/adagrams'; +import Model from "demo/model"; +import Adagrams from "demo/adagrams"; -describe.skip('Game Model', () => { +describe("Game Model", () => { const config = { - players: [ - 'Player A', - 'Player B', - ], + players: ["Player A", "Player B"], rounds: 3, time: 60, // Seconds }; - describe('constructor', () => { - it('creates a new Model instance', () => { + describe("constructor", () => { + it("creates a new Model instance", () => { const model = new Model(config); expect(model).toBeInstanceOf(Model); }); - it('requires a config parameter', () => { + it("requires a config parameter", () => { expect(() => { const model = new Model(); }).toThrow(/config/); }); - it('initializes the round number to zero', () => { + it("initializes the round number to zero", () => { const model = new Model(config); expect(model.round).toBe(0); }); - it('initializes the current player to null', () => { + it("initializes the current player to null", () => { const model = new Model(config); expect(model.currentPlayer).toBe(null); }); - it('initializes the letter bank to null', () => { + it("initializes the letter bank to null", () => { const model = new Model(config); expect(model.letterBank).toBe(null); }); - it('initializes the plays history', () => { + it("initializes the plays history", () => { const model = new Model(config); expect(model.plays).toBeInstanceOf(Object); @@ -54,14 +51,14 @@ describe.skip('Game Model', () => { }); }); - describe('.currentPlayerName()', () => { - it('is defined', () => { + describe(".currentPlayerName()", () => { + it("is defined", () => { const model = new Model(config); expect(model.currentPlayerName).toBeDefined(); }); - it('returns the name of the current player when game is on-going', () => { + it("returns the name of the current player when game is on-going", () => { const model = new Model(config); model.nextRound(); @@ -69,21 +66,21 @@ describe.skip('Game Model', () => { expect(model.currentPlayerName()).toEqual(model.config.players[0]); }); - it('returns null when the game is not on-going', () => { + it("returns null when the game is not on-going", () => { const model = new Model(config); expect(model.currentPlayerName()).toBe(null); }); }); - describe('.nextRound', () => { - it('is defined', () => { + describe(".nextRound", () => { + it("is defined", () => { const model = new Model(config); expect(model.nextRound).toBeDefined(); }); - it('increments the round number', () => { + it("increments the round number", () => { const model = new Model(config); const roundBefore = model.round; @@ -92,7 +89,7 @@ describe.skip('Game Model', () => { expect(model.round).toBe(roundBefore + 1); }); - it('initializes the current player number to first player', () => { + it("initializes the current player number to first player", () => { const model = new Model(config); model.nextRound(); @@ -100,7 +97,7 @@ describe.skip('Game Model', () => { expect(model.currentPlayer).toBe(0); }); - it('initializes the round play history for first player', () => { + it("initializes the round play history for first player", () => { const model = new Model(config); model.nextRound(); @@ -113,7 +110,7 @@ describe.skip('Game Model', () => { }); }); - it('draws a new hand of letters', () => { + it("draws a new hand of letters", () => { const model = new Model(config); model.nextRound(); @@ -125,8 +122,8 @@ describe.skip('Game Model', () => { }); }); - describe('returns game state', () => { - it('gameOver', () => { + describe("returns game state", () => { + it("gameOver", () => { const model = new Model({ ...config, rounds: 1 }); const gameState = model.nextRound(); @@ -138,8 +135,8 @@ describe.skip('Game Model', () => { expect(gameOverState.gameOver).toBe(true); }); - it('winner', () => { - const model = new Model({ ...config, rounds : 2 }); + it("winner", () => { + const model = new Model({ ...config, rounds: 2 }); // Start game, no one has won yet let gameState = model.nextRound(); @@ -147,7 +144,7 @@ describe.skip('Game Model', () => { // First player plays a word let p1Score = 0; - let word = model.letterBank.slice(0, 5).join(''); + let word = model.letterBank.slice(0, 5).join(""); p1Score += model.playWord(word); // Second player does not play @@ -157,7 +154,7 @@ describe.skip('Game Model', () => { expect(gameState.winner).toBe(null); // First player plays another word - word = model.letterBank.slice(0, 5).join(''); + word = model.letterBank.slice(0, 5).join(""); p1Score += model.playWord(word); // Second player does not play again @@ -173,7 +170,7 @@ describe.skip('Game Model', () => { }); }); - describe('.nextTurn', () => { + describe(".nextTurn", () => { const getModel = () => { const model = new Model(config); model.nextRound(); @@ -181,13 +178,13 @@ describe.skip('Game Model', () => { return model; }; - it('is defined', () => { + it("is defined", () => { const model = getModel(); expect(model.nextTurn).toBeDefined(); }); - it('increments the current player index', () => { + it("increments the current player index", () => { const model = getModel(); const origPlayer = model.currentPlayer; @@ -198,8 +195,8 @@ describe.skip('Game Model', () => { expect(model.currentPlayer).toBe(origPlayer + 2); }); - describe('returns round state', () => { - it('roundOver', () => { + describe("returns round state", () => { + it("roundOver", () => { const model = getModel(); const roundState = model.nextTurn(); @@ -208,7 +205,7 @@ describe.skip('Game Model', () => { expect(config.players.length).toBeGreaterThan(1); expect(roundState).toBeInstanceOf(Object); - expect(roundState).toHaveProperty('roundOver'); + expect(roundState).toHaveProperty("roundOver"); expect(roundState.roundOver).toBe(false); // Advance to the final turn @@ -221,7 +218,7 @@ describe.skip('Game Model', () => { expect(roundOverState.roundOver).toBe(true); }); - it('winner', () => { + it("winner", () => { const model = getModel(); const roundState = model.nextTurn(); @@ -236,7 +233,7 @@ describe.skip('Game Model', () => { }); // Play a word as the last player - const word = model.letterBank.slice(0, 5).join(''); + const word = model.letterBank.slice(0, 5).join(""); const score = model.playWord(word); // Complete the final turn, round is over and winner should be set @@ -253,7 +250,7 @@ describe.skip('Game Model', () => { }); }); - describe('.playWord', () => { + describe(".playWord", () => { const getModel = () => { const model = new Model(config); model.nextRound(); @@ -265,18 +262,18 @@ describe.skip('Game Model', () => { return [...(model.plays[player][round - 1] || [])]; }; - it('is defined', () => { + it("is defined", () => { const model = getModel(); expect(model.playWord).toBeDefined(); }); - describe('for valid words', () => { + describe("for valid words", () => { const getWord = (model) => { - return model.letterBank.slice(0, 5).join(''); + return model.letterBank.slice(0, 5).join(""); }; - it('it returns the word score', () => { + it("it returns the word score", () => { const model = getModel(); const word = getWord(model); const score = Adagrams.scoreWord(word); @@ -284,21 +281,28 @@ describe.skip('Game Model', () => { expect(model.playWord(word)).toBe(score); }); - it('adds word to plays history for current player', () => { + it("adds word to plays history for current player", () => { const model = getModel(); const player = model.currentPlayerName(); const origPlays = getPlays(model, player, model.round); const word1 = getWord(model); model.playWord(word1); - expect(getPlays(model, player, model.round)).toEqual([...origPlays, word1]); + expect(getPlays(model, player, model.round)).toEqual([ + ...origPlays, + word1, + ]); const word2 = getWord(model); model.playWord(word2); - expect(getPlays(model, player, model.round)).toEqual([...origPlays, word1, word2]); + expect(getPlays(model, player, model.round)).toEqual([ + ...origPlays, + word1, + word2, + ]); }); - it('validates word case-insensitively', () => { + it("validates word case-insensitively", () => { const model = getModel(); const word = getWord(model); const score = Adagrams.scoreWord(word); @@ -307,27 +311,29 @@ describe.skip('Game Model', () => { }); }); - describe('for invalid words', () => { + describe("for invalid words", () => { const getWord = (model) => { const letter = model.letterBank[0]; - return letter.repeat(model.letterBank.filter((l) => { - return l === letter; - }).length + 1); + return letter.repeat( + model.letterBank.filter((l) => { + return l === letter; + }).length + 1 + ); }; - it('it returns null', () => { + it("it returns null", () => { const model = getModel(); const word = getWord(model); expect(model.playWord(word)).toBe(null); - expect(model.playWord('123')).toBe(null); - expect(model.playWord('')).toBe(null); + expect(model.playWord("123")).toBe(null); + expect(model.playWord("")).toBe(null); }); - it('does not add word to history', () => { + it("does not add word to history", () => { const model = getModel(); const word = getWord(model); - const origPlays = {...model.plays}; + const origPlays = { ...model.plays }; model.playWord(word); From 1c63b003f36f58770a8acceb5bb23830fe1a75f4 Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 13:48:41 -0800 Subject: [PATCH 08/14] Created Adagrams class and implemented drawletters function --- src/adagrams.js | 106 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 4a271890..b5b59210 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -57,44 +57,44 @@ const scoreChart = { }; const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -export const drawLetters = () => { - let availableLetters = { ...letterPool }; - const lettersInHand = []; +// export const drawLetters = () => { +// let availableLetters = { ...letterPool }; +// const lettersInHand = []; - while (lettersInHand.length < 10) { - const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; +// while (lettersInHand.length < 10) { +// const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; - if (availableLetters[letter] > 0) { - availableLetters[letter]--; - lettersInHand.push(letter); - } - } - return lettersInHand; -}; +// if (availableLetters[letter] > 0) { +// availableLetters[letter]--; +// lettersInHand.push(letter); +// } +// } +// return lettersInHand; +// }; -export const usesAvailableLetters = (input, lettersInHand) => { - const drawnDict = {}; +// export const usesAvailableLetters = (input, lettersInHand) => { +// const drawnDict = {}; - lettersInHand.forEach((letter) => { - if (letter in drawnDict) { - drawnDict[letter]++; - } else { - drawnDict[letter] = 1; - } - }); +// lettersInHand.forEach((letter) => { +// if (letter in drawnDict) { +// drawnDict[letter]++; +// } else { +// drawnDict[letter] = 1; +// } +// }); - const word = [...input.toUpperCase()]; +// const word = [...input.toUpperCase()]; - for (const letter of word) { - if (drawnDict[letter] == 0 || !drawnDict[letter]) { - return false; - } else { - drawnDict[letter]--; - } - } +// for (const letter of word) { +// if (drawnDict[letter] == 0 || !drawnDict[letter]) { +// return false; +// } else { +// drawnDict[letter]--; +// } +// } - return true; -}; +// return true; +// }; export const scoreWord = (word) => { const wordList = [...word.toUpperCase()]; @@ -129,3 +129,47 @@ export const highestScoreFrom = (words) => { const winner = { word: winningWord, score: highestScore }; return winner; }; + +// ------------ Wave 05--------------------------- +class Adagrams { + constructor(input) { + this.input = input; + } + drawLetters() { + let availableLetters = { ...letterPool }; + const lettersInHand = []; + + while (lettersInHand.length < 10) { + const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; + + if (availableLetters[letter] > 0) { + availableLetters[letter]--; + lettersInHand.push(letter); + } + } + return lettersInHand; + } + + usesAvailableLetters(this.input, lettersInHand) { + const drawnDict = {}; + + lettersInHand.forEach((letter) => { + if (letter in drawnDict) { + drawnDict[letter]++; + } else { + drawnDict[letter] = 1; + } + }); + const word = [...input.toUpperCase()]; + + for (const letter of word) { + if (drawnDict[letter] == 0 || !drawnDict[letter]) { + return false; + } else { + drawnDict[letter]--; + } + } + + return true; + }; +} From da381341f20ba273e00887481ee57a6980a29f13 Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 14:21:42 -0800 Subject: [PATCH 09/14] attempted to modify tests --- src/adagrams.js | 49 +++++++++++++++++++++---------------------- test/adagrams.test.js | 11 ++++++---- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index b5b59210..aad61581 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -132,9 +132,6 @@ export const highestScoreFrom = (words) => { // ------------ Wave 05--------------------------- class Adagrams { - constructor(input) { - this.input = input; - } drawLetters() { let availableLetters = { ...letterPool }; const lettersInHand = []; @@ -150,26 +147,28 @@ class Adagrams { return lettersInHand; } - usesAvailableLetters(this.input, lettersInHand) { - const drawnDict = {}; - - lettersInHand.forEach((letter) => { - if (letter in drawnDict) { - drawnDict[letter]++; - } else { - drawnDict[letter] = 1; - } - }); - const word = [...input.toUpperCase()]; - - for (const letter of word) { - if (drawnDict[letter] == 0 || !drawnDict[letter]) { - return false; - } else { - drawnDict[letter]--; - } - } - - return true; - }; + // usesAvailableLetters(this.input, lettersInHand) { + // const drawnDict = {}; + + // lettersInHand.forEach((letter) => { + // if (letter in drawnDict) { + // drawnDict[letter]++; + // } else { + // drawnDict[letter] = 1; + // } + // }); + // const word = [...input.toUpperCase()]; + + // for (const letter of word) { + // if (drawnDict[letter] == 0 || !drawnDict[letter]) { + // return false; + // } else { + // drawnDict[letter]--; + // } + // } + + // return true; + // }; } + +export default Adagrams; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index fc35fcf9..e7ca7da5 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -3,6 +3,7 @@ import { usesAvailableLetters, scoreWord, highestScoreFrom, + Adagrams, } from "adagrams"; const LETTER_POOL = { @@ -36,14 +37,16 @@ const LETTER_POOL = { describe("Adagrams", () => { describe("drawLetters", () => { - it("draws ten letters from the letter pool", () => { - const drawn = drawLetters(); + const newAdagram = new Adagrams(); + + it("draws ten letters from the letter pool", (newAdagram) => { + const drawn = newAdagram.drawLetters(); expect(drawn).toHaveLength(10); }); it("returns an array, and each item is a single-letter string", () => { - const drawn = drawLetters(); + const drawn = newAdagram.drawLetters(); expect(Array.isArray(drawn)).toBe(true); drawn.forEach((l) => { @@ -53,7 +56,7 @@ describe("Adagrams", () => { it("does not draw a letter too many times", () => { for (let i = 0; i < 1000; i++) { - const drawn = drawLetters(); + const drawn = newAdagrams.drawLetters(); const letter_freq = {}; for (let letter of drawn) { if (letter in letter_freq) { From cf9478e438a541bddf0d4a6cfce4511cbddedfbf Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 15:09:14 -0800 Subject: [PATCH 10/14] passed tests for drawLetters() function --- src/adagrams.js | 45 ++++++++++++++++++++++--------------------- test/adagrams.test.js | 17 ++++++++-------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index aad61581..fae0c4da 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -147,28 +147,29 @@ class Adagrams { return lettersInHand; } - // usesAvailableLetters(this.input, lettersInHand) { - // const drawnDict = {}; - - // lettersInHand.forEach((letter) => { - // if (letter in drawnDict) { - // drawnDict[letter]++; - // } else { - // drawnDict[letter] = 1; - // } - // }); - // const word = [...input.toUpperCase()]; - - // for (const letter of word) { - // if (drawnDict[letter] == 0 || !drawnDict[letter]) { - // return false; - // } else { - // drawnDict[letter]--; - // } - // } - - // return true; - // }; + static usesAvailableLetters(input, Adagrams) { + const drawnDict = {}; + const lettersInHand = Adagrams.drawLetters(); + + lettersInHand.forEach((letter) => { + if (letter in drawnDict) { + drawnDict[letter]++; + } else { + drawnDict[letter] = 1; + } + }); + const word = [...input.toUpperCase()]; + + for (const letter of word) { + if (drawnDict[letter] == 0 || !drawnDict[letter]) { + return false; + } else { + drawnDict[letter]--; + } + } + + return true; + } } export default Adagrams; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index e7ca7da5..cf591057 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -3,9 +3,8 @@ import { usesAvailableLetters, scoreWord, highestScoreFrom, - Adagrams, } from "adagrams"; - +import Adagrams from "../src/adagrams"; const LETTER_POOL = { A: 9, B: 2, @@ -39,7 +38,7 @@ describe("Adagrams", () => { describe("drawLetters", () => { const newAdagram = new Adagrams(); - it("draws ten letters from the letter pool", (newAdagram) => { + it("draws ten letters from the letter pool", () => { const drawn = newAdagram.drawLetters(); expect(drawn).toHaveLength(10); @@ -56,7 +55,7 @@ describe("Adagrams", () => { it("does not draw a letter too many times", () => { for (let i = 0; i < 1000; i++) { - const drawn = newAdagrams.drawLetters(); + const drawn = newAdagram.drawLetters(); const letter_freq = {}; for (let letter of drawn) { if (letter in letter_freq) { @@ -74,11 +73,13 @@ describe("Adagrams", () => { }); describe("usesAvailableLetters", () => { + const newAdagram = new Adagrams(); it("returns true if the submitted letters are valid against the drawn letters", () => { - const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; + // const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; + // const drawn = newAdagram.drawLetters(); const word = "DOG"; - const isValid = usesAvailableLetters(word, drawn); + const isValid = Adagrams.usesAvailableLetters(word, newAdagram); expect(isValid).toBe(true); }); @@ -86,7 +87,7 @@ describe("Adagrams", () => { const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"]; const word = "DOG"; - const isValid = usesAvailableLetters(word, drawn); + const isValid = newAdagram.usesAvailableLetters(word, drawn); expect(isValid).toBe(false); }); @@ -94,7 +95,7 @@ describe("Adagrams", () => { const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; const word = "GOOD"; - const isValid = usesAvailableLetters(word, drawn); + const isValid = newAdagram.usesAvailableLetters(word, drawn); expect(isValid).toBe(false); }); }); From 33d5710170409ddb2bcc0e4daa965ba03d10f41d Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 15:22:07 -0800 Subject: [PATCH 11/14] Changed uses available letters to static method --- test/adagrams.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/adagrams.test.js b/test/adagrams.test.js index cf591057..54968e95 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -73,10 +73,11 @@ describe("Adagrams", () => { }); describe("usesAvailableLetters", () => { - const newAdagram = new Adagrams(); it("returns true if the submitted letters are valid against the drawn letters", () => { // const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; // const drawn = newAdagram.drawLetters(); + const newAdagram = new Adagrams(); + const word = "DOG"; const isValid = Adagrams.usesAvailableLetters(word, newAdagram); @@ -88,6 +89,7 @@ describe("Adagrams", () => { const word = "DOG"; const isValid = newAdagram.usesAvailableLetters(word, drawn); + console.log(isValid); expect(isValid).toBe(false); }); From 17bb45575d7f1dd836ed6c0609a8b26e2b712f93 Mon Sep 17 00:00:00 2001 From: Tasha Date: Fri, 2 Dec 2022 15:25:41 -0800 Subject: [PATCH 12/14] Modified testing to fix errors from Wave 5 --- test/adagrams.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/adagrams.test.js b/test/adagrams.test.js index fc35fcf9..c3c7a5ac 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -73,6 +73,7 @@ describe("Adagrams", () => { describe("usesAvailableLetters", () => { it("returns true if the submitted letters are valid against the drawn letters", () => { const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; + const word = "DOG"; const isValid = usesAvailableLetters(word, drawn); @@ -84,6 +85,7 @@ describe("Adagrams", () => { const word = "DOG"; const isValid = usesAvailableLetters(word, drawn); + console.log(isValid); expect(isValid).toBe(false); }); From 13ce9996f70b6b426eb5605817534764e2787020 Mon Sep 17 00:00:00 2001 From: Tasha Date: Tue, 6 Dec 2022 09:37:26 -0800 Subject: [PATCH 13/14] Changed all class methods to be static, and updated test files to use class methods. Passed all tests in adagrams.test.js --- src/adagrams.js | 117 +++++++++++++++--------------------------- test/adagrams.test.js | 60 +++++++++------------- 2 files changed, 65 insertions(+), 112 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index fae0c4da..0038f61b 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -57,82 +57,9 @@ const scoreChart = { }; const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -// export const drawLetters = () => { -// let availableLetters = { ...letterPool }; -// const lettersInHand = []; - -// while (lettersInHand.length < 10) { -// const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; - -// if (availableLetters[letter] > 0) { -// availableLetters[letter]--; -// lettersInHand.push(letter); -// } -// } -// return lettersInHand; -// }; - -// export const usesAvailableLetters = (input, lettersInHand) => { -// const drawnDict = {}; - -// lettersInHand.forEach((letter) => { -// if (letter in drawnDict) { -// drawnDict[letter]++; -// } else { -// drawnDict[letter] = 1; -// } -// }); - -// const word = [...input.toUpperCase()]; - -// for (const letter of word) { -// if (drawnDict[letter] == 0 || !drawnDict[letter]) { -// return false; -// } else { -// drawnDict[letter]--; -// } -// } - -// return true; -// }; - -export const scoreWord = (word) => { - const wordList = [...word.toUpperCase()]; - let score = 0; - if (wordList.length >= 7) { - score += 8; - } - - wordList.forEach((letter) => (score += scoreChart[letter])); - return score; -}; - -export const highestScoreFrom = (words) => { - let highestScore = 0; - let winningWord = ""; - - words.forEach((word) => { - const score = scoreWord(word); - - if (score > highestScore) { - highestScore = score; - winningWord = word; - } else if (score === highestScore) { - if (word.length === 10 && winningWord.length != 10) { - winningWord = word; - } else if (word.length < winningWord.length && winningWord.length != 10) { - winningWord = word; - } - } - }); - - const winner = { word: winningWord, score: highestScore }; - return winner; -}; - // ------------ Wave 05--------------------------- class Adagrams { - drawLetters() { + static drawLetters() { let availableLetters = { ...letterPool }; const lettersInHand = []; @@ -147,10 +74,8 @@ class Adagrams { return lettersInHand; } - static usesAvailableLetters(input, Adagrams) { + static usesAvailableLetters(input, lettersInHand) { const drawnDict = {}; - const lettersInHand = Adagrams.drawLetters(); - lettersInHand.forEach((letter) => { if (letter in drawnDict) { drawnDict[letter]++; @@ -170,6 +95,44 @@ class Adagrams { return true; } + + static scoreWord(word) { + const wordList = [...word.toUpperCase()]; + let score = 0; + if (wordList.length >= 7) { + score += 8; + } + + wordList.forEach((letter) => (score += scoreChart[letter])); + + return score; + } + + static highestScoreFrom(words) { + let highestScore = 0; + let winningWord = ""; + + words.forEach((word) => { + const score = Adagrams.scoreWord(word); + + if (score > highestScore) { + highestScore = score; + winningWord = word; + } else if (score === highestScore) { + if (word.length === 10 && winningWord.length != 10) { + winningWord = word; + } else if ( + word.length < winningWord.length && + winningWord.length != 10 + ) { + winningWord = word; + } + } + }); + + const winner = { word: winningWord, score: highestScore }; + return winner; + } } export default Adagrams; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 54968e95..f55309b8 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -1,10 +1,4 @@ -import { - drawLetters, - usesAvailableLetters, - scoreWord, - highestScoreFrom, -} from "adagrams"; -import Adagrams from "../src/adagrams"; +import Adagrams from "adagrams"; const LETTER_POOL = { A: 9, B: 2, @@ -36,16 +30,16 @@ const LETTER_POOL = { describe("Adagrams", () => { describe("drawLetters", () => { - const newAdagram = new Adagrams(); + // const newAdagram = new Adagrams() it("draws ten letters from the letter pool", () => { - const drawn = newAdagram.drawLetters(); + const drawn = Adagrams.drawLetters(); expect(drawn).toHaveLength(10); }); it("returns an array, and each item is a single-letter string", () => { - const drawn = newAdagram.drawLetters(); + const drawn = Adagrams.drawLetters(); expect(Array.isArray(drawn)).toBe(true); drawn.forEach((l) => { @@ -55,7 +49,7 @@ describe("Adagrams", () => { it("does not draw a letter too many times", () => { for (let i = 0; i < 1000; i++) { - const drawn = newAdagram.drawLetters(); + const drawn = Adagrams.drawLetters(); const letter_freq = {}; for (let letter of drawn) { if (letter in letter_freq) { @@ -74,13 +68,9 @@ describe("Adagrams", () => { describe("usesAvailableLetters", () => { it("returns true if the submitted letters are valid against the drawn letters", () => { - // const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; - // const drawn = newAdagram.drawLetters(); - const newAdagram = new Adagrams(); - + const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; const word = "DOG"; - - const isValid = Adagrams.usesAvailableLetters(word, newAdagram); + const isValid = Adagrams.usesAvailableLetters(word, drawn); expect(isValid).toBe(true); }); @@ -88,7 +78,7 @@ describe("Adagrams", () => { const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"]; const word = "DOG"; - const isValid = newAdagram.usesAvailableLetters(word, drawn); + const isValid = Adagrams.usesAvailableLetters(word, drawn); console.log(isValid); expect(isValid).toBe(false); }); @@ -97,7 +87,7 @@ describe("Adagrams", () => { const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; const word = "GOOD"; - const isValid = newAdagram.usesAvailableLetters(word, drawn); + const isValid = Adagrams.usesAvailableLetters(word, drawn); expect(isValid).toBe(false); }); }); @@ -105,7 +95,7 @@ describe("Adagrams", () => { describe("scoreWord", () => { const expectScores = (wordScores) => { Object.entries(wordScores).forEach(([word, score]) => { - expect(scoreWord(word)).toBe(score); + expect(Adagrams.scoreWord(word)).toBe(score); }); }; @@ -144,21 +134,21 @@ describe("Adagrams", () => { 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") }; + const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; - expect(highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); }); it("accurately finds best scoring word even if not sorted", () => { const words = ["XXX", "XXXX", "X", "XX"]; - const correct = { word: "XXXX", score: scoreWord("XXXX") }; + const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; - expect(highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); }); describe("in case of tied score", () => { const expectTie = (words) => { - const scores = words.map((word) => scoreWord(word)); + const scores = words.map((word) => Adagrams.scoreWord(word)); const highScore = scores.reduce((h, s) => (h < s ? s : h), 0); const tiedWords = scores.filter((s) => s == highScore); @@ -170,37 +160,37 @@ describe("Adagrams", () => { const words = ["AAAAAAAAAA", "BBBBBB"]; const correct = { word: "AAAAAAAAAA", - score: scoreWord("AAAAAAAAAA"), + score: Adagrams.scoreWord("AAAAAAAAAA"), }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(correct); - expect(highestScoreFrom(words.reverse())).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); }); it("selects the word with fewer letters when neither are 10 letters", () => { const words = ["MMMM", "WWW"]; - const correct = { word: "WWW", score: scoreWord("WWW") }; + const correct = { word: "WWW", score: Adagrams.scoreWord("WWW") }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(correct); - expect(highestScoreFrom(words.reverse())).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); }); it("selects the first word when both have same length", () => { const words = ["AAAAAAAAAA", "EEEEEEEEEE"]; const first = { word: "AAAAAAAAAA", - score: scoreWord("AAAAAAAAAA"), + score: Adagrams.scoreWord("AAAAAAAAAA"), }; const second = { word: "EEEEEEEEEE", - score: scoreWord("EEEEEEEEEE"), + score: Adagrams.scoreWord("EEEEEEEEEE"), }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(first); - expect(highestScoreFrom(words.reverse())).toEqual(second); + expect(Adagrams.highestScoreFrom(words)).toEqual(first); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(second); }); }); }); From 7734f543c9f0c66e9ce4446001234a8b60992da2 Mon Sep 17 00:00:00 2001 From: Tasha Date: Tue, 6 Dec 2022 16:35:05 -0800 Subject: [PATCH 14/14] Added .skip to model test files --- test/demo/model.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/demo/model.test.js b/test/demo/model.test.js index 9616ec8b..a5f2a89b 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("Game Model", () => { +describe.skip("Game Model", () => { const config = { players: ["Player A", "Player B"], rounds: 3,