From 7722768ac982fa2f4a0cf0e3769a3c71ca401dcc Mon Sep 17 00:00:00 2001 From: somy Date: Tue, 26 Nov 2024 09:44:16 -0800 Subject: [PATCH 1/3] Complete wave 1 and 2 --- README.md | 4 ++-- src/adagrams.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cb3b2afc..b016ff3d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - Writing Javascript functions with correct syntax, including: - only use `const` and `let` variables - use semi-colons where needed - - name variables and functions with camelCase + - name variables and functions with camelCase÷ - Practicing good git hygiene: - make at least three small commits with meaningful commit messages - Practicing TDD with JavaScript and the Jest testing framework @@ -18,7 +18,7 @@ A test suite and sample game project is provided in order to practice TDD and ve Note there are a handful of incomplete tests that currently throw exceptions. You should complete these tests and remove the exception. -## Getting Started +## Getting ∏ After forking and cloning this repo you should `cd` to the project directory and run: diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..4f55f02b 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,11 +1,62 @@ export const drawLetters = () => { + 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 +}; + +// Create a list of letters based on the pool +let lettersList = []; +for (const [letter, count] of Object.entries(letterPool)) { + for (let i = 0; i < count; i++) { + lettersList.push(letter); + } +} + +// Generate a hand of 10 random letters +let hand = []; +let temporaryLettersList = [...lettersList]; + +for (let i = 0; i < 10; i++) { + const randomIndex = Math.floor(Math.random() * temporaryLettersList.length); + + // Remove the letter from the list + const randomLetter = temporaryLettersList.splice(randomIndex, 1)[0]; + hand.push(randomLetter); +} + +return hand; // Implement this method for wave 1 }; export const usesAvailableLetters = (input, lettersInHand) => { // Implement this method for wave 2 + // Loop through each letter in the input string + for (let i = 0; i < input.length; i++) { + const letter = input[i]; + + // Check if the letter is in lettersInHand + if (!lettersInHand.includes(letter)) { + return false; // Return false if any letter is not in the hand + } + + // If the letter is in the hand, remove it from the hand (so it can't be reused) + // This ensures that a letter can only be used as many times as it appears in the hand + const index = lettersInHand.indexOf(letter); + lettersInHand.splice(index, 1); // Remove the letter from the hand + } + + // If we made it here, it means all letters in input are available in the hand + return true; }; + + + + + + export const scoreWord = (word) => { // Implement this method for wave 3 }; From 520e102dc531739891a74cc071931ec808fac8fb Mon Sep 17 00:00:00 2001 From: somy Date: Tue, 26 Nov 2024 21:12:43 -0800 Subject: [PATCH 2/3] wave 3 and 4 --- src/adagrams.js | 88 +++++++++++++++++++++++++++++++++---------- test/adagrams.test.js | 8 ++-- 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 4f55f02b..7312a920 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,10 +1,23 @@ +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 +}; + + //Map of points + const letterValue = new Map([ + [['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1], + [['D', 'G'], 2], + [['B', 'C', 'M', 'P'], 3], + [['F', 'H', 'V', 'W', 'Y'], 4], + [['K'], 5], + [['J', 'X'], 8], + [['Q', 'Z'], 10] + ]); + + export const drawLetters = () => { - 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 -}; // Create a list of letters based on the pool let lettersList = []; @@ -25,42 +38,77 @@ for (let i = 0; i < 10; i++) { const randomLetter = temporaryLettersList.splice(randomIndex, 1)[0]; hand.push(randomLetter); } - return hand; - // Implement this method for wave 1 }; export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 - // Loop through each letter in the input string for (let i = 0; i < input.length; i++) { const letter = input[i]; - // Check if the letter is in lettersInHand if (!lettersInHand.includes(letter)) { - return false; // Return false if any letter is not in the hand + return false; } - // If the letter is in the hand, remove it from the hand (so it can't be reused) - // This ensures that a letter can only be used as many times as it appears in the hand const index = lettersInHand.indexOf(letter); - lettersInHand.splice(index, 1); // Remove the letter from the hand + lettersInHand.splice(index, 1); } - // If we made it here, it means all letters in input are available in the hand return true; }; +export const scoreWord = (word) => { + if (word.length === 0){ + return 0; + } + let totalScore = 0 + for (let i = 0; i < word.length; i++) { + let letter = word[i].toUpperCase(); -export const scoreWord = (word) => { - // Implement this method for wave 3 + for (let [letters, score] of letterValue) { + if (letters.includes(letter)) { + totalScore += score; + break; + } + } + } + if (word.length > 6){ + totalScore += 8 + } + return totalScore }; - export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + + if (words.length === 0) { + return 0; + } + + let winner = { word: '', score: 0 }; + + for (let word of words) { + let currentScore = scoreWord(word); + + if (currentScore > winner.score) { + winner = { word, score: currentScore }; + } + + else if (currentScore === winner.score) { + if (word.length === 10 && winner.word.length !== 10) { + winner = { word, score: currentScore }; + } + + else if ( + word.length !== 10 && + winner.word.length !== 10 && + word.length < winner.word.length + ) { + winner = { word, score: currentScore }; + } + } + } + return winner; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..46a6e8ea 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", () => { From f646712c65f116f097316c5babf9aee08d3cd422 Mon Sep 17 00:00:00 2001 From: somy Date: Tue, 26 Nov 2024 21:19:53 -0800 Subject: [PATCH 3/3] delete unnecessary spaces --- src/adagrams.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 7312a920..d2bee92d 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -5,21 +5,19 @@ const letterPool = { 'V': 2, 'W': 2, 'X': 1, 'Y': 2, 'Z': 1 }; - //Map of points - const letterValue = new Map([ - [['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1], - [['D', 'G'], 2], - [['B', 'C', 'M', 'P'], 3], - [['F', 'H', 'V', 'W', 'Y'], 4], - [['K'], 5], - [['J', 'X'], 8], - [['Q', 'Z'], 10] - ]); +const letterValue = new Map([ + [['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1], + [['D', 'G'], 2], + [['B', 'C', 'M', 'P'], 3], + [['F', 'H', 'V', 'W', 'Y'], 4], + [['K'], 5], + [['J', 'X'], 8], + [['Q', 'Z'], 10] +]); export const drawLetters = () => { -// Create a list of letters based on the pool let lettersList = []; for (const [letter, count] of Object.entries(letterPool)) { for (let i = 0; i < count; i++) { @@ -27,14 +25,11 @@ for (const [letter, count] of Object.entries(letterPool)) { } } -// Generate a hand of 10 random letters let hand = []; let temporaryLettersList = [...lettersList]; for (let i = 0; i < 10; i++) { const randomIndex = Math.floor(Math.random() * temporaryLettersList.length); - - // Remove the letter from the list const randomLetter = temporaryLettersList.splice(randomIndex, 1)[0]; hand.push(randomLetter); } @@ -52,15 +47,11 @@ export const usesAvailableLetters = (input, lettersInHand) => { const index = lettersInHand.indexOf(letter); lettersInHand.splice(index, 1); } - return true; }; - export const scoreWord = (word) => { - - if (word.length === 0){ return 0; } @@ -81,6 +72,8 @@ export const scoreWord = (word) => { } return totalScore }; + + export const highestScoreFrom = (words) => { if (words.length === 0) {