From 0146a515a321618bc71d0c43427a171b175da184 Mon Sep 17 00:00:00 2001 From: Anees Quateja Date: Mon, 25 Nov 2024 21:45:40 -0800 Subject: [PATCH 1/4] implemented wave1 --- src/adagrams.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..75115e17 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,9 +1,59 @@ +// import { random } from "core-js/core/number"; + export const drawLetters = () => { - // Implement this method for wave 1 + // wave 1 + // Step 1: Define the letterDistribution as an object + const letterDistribution = { + 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 letters = Object.keys(letterDistribution); // ['A', 'B', 'C', ...] + const hand = []; + const letterCount = { ...letterDistribution }; // Copy of the distribution ... is called the spread operator.It takes all the key-value pairs from an object (or all elements from an array) and "spreads" them into a new object (or array). + + + while (hand.length < 10) { + const randomIndex = Math.floor(Math.random() * letters.length); // 0.23*26=5.98 =>5 + const randomLetter = letters[randomIndex]; + + if (letterCount[randomLetter] > 0) { + hand.push(randomLetter); + letterCount[randomLetter]--; // Reduce available count so -- is the decrement operator + } + } + + return hand; }; -export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 + + // Implement this method for wave 2 + export const usesAvailableLetters = (input, lettersInHand) => { + }; export const scoreWord = (word) => { From bfb9149c5c2e289badbc02edcc21660f7ddaf9cf Mon Sep 17 00:00:00 2001 From: Anees Quateja Date: Mon, 25 Nov 2024 22:10:26 -0800 Subject: [PATCH 2/4] implemented wave_2 --- src/adagrams.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/adagrams.js b/src/adagrams.js index 75115e17..c99067cb 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -51,8 +51,25 @@ export const drawLetters = () => { }; - // Implement this method for wave 2 + // Implement this method for wave 2 export const usesAvailableLetters = (input, lettersInHand) => { + // Step 1: Create a copy of lettersInHand to avoid modifying the original array + const handCopy = [...lettersInHand]; + + // Step 2: Iterate over each letter in the input + for (const letter of input) { + const index = handCopy.indexOf(letter); // Find the index of the letter in the hand copy + if (index === -1) { + // If the letter is not found, return false + return false; + } else { + // If the letter is found, remove it from the hand copy + handCopy.splice(index, 1); + } + } + + // Step 3: If all letters in the input were found, return true + return true; }; From 304e15ecf17af3e6e6e20451419a041ff53bfdec Mon Sep 17 00:00:00 2001 From: Anees Quateja Date: Tue, 26 Nov 2024 18:00:35 -0800 Subject: [PATCH 3/4] implemeted wave_3 and wave_4 with all test passing --- src/adagrams.js | 63 +++++++++++++++++++++++++++++++++++++++++-- test/adagrams.test.js | 8 +++--- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index c99067cb..8b1c53f1 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -73,10 +73,69 @@ export const drawLetters = () => { }; + // Implement this method for wave 3 export const scoreWord = (word) => { - // Implement this method for wave 3 + // Step 1: Define the score chart + const scoreChart = { + A: 1, E: 1, I: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1, + D: 2, G: 2, + B: 3, C: 3, M: 3, P: 3, + F: 4, H: 4, V: 4, W: 4, Y: 4, + K: 5, + J: 8, X: 8, + Q: 10, Z: 10, + }; + + // Step 2: Initialize total score + let totalScore = 0; + + // Step 3: Iterate over each letter in the word + for (const letter of word.toUpperCase()) { + totalScore += scoreChart[letter] || 0; // Add score; default to 0 if letter not found + } + + // Step 4: Add bonus points for word length + if (word.length >= 7 && word.length <= 10) { + totalScore += 8; + } + + // Step 5: Return the total score + return totalScore; }; + + // Implement this method for wave 4 export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + let bestWord = null; // Will hold the winning word object + let highestScore = 0; // Track the highest score + + for (const word of words) { + const score = scoreWord(word); // Calculate score using scoreWord from Wave 3 + + // If the current word has a higher score, update the bestWord + if (score > highestScore) { + bestWord = { word, score }; + highestScore = score; + } else if (score === highestScore) { + // Tie-breaking rules + const isCurrentWordTenLetters = word.length === 10; + const isBestWordTenLetters = bestWord.word.length === 10; + + if (isCurrentWordTenLetters && !isBestWordTenLetters) { + // Prefer the word with 10 letters + bestWord = { word, score }; + } else if ( + !isCurrentWordTenLetters && + !isBestWordTenLetters && + word.length < bestWord.word.length + ) { + // Prefer the shorter word (if no 10-letter words involved) + bestWord = { word, score }; + } + // If both words are the same length and score, keep the first word + // (which is already stored in bestWord) + } + } + + return bestWord; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..4258f3c5 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,8 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - throw "Complete test"; + expect(scoreWord("")).toBe(0); + // throw "Complete test"; }); it("adds an extra 8 points if word is 7 or more characters long", () => { @@ -133,7 +134,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 +146,8 @@ describe("Adagrams", () => { const words = ["XXX", "XXXX", "X", "XX"]; const correct = { word: "XXXX", score: scoreWord("XXXX") }; - throw "Complete test by adding an assertion"; + // throw "Complete test by adding an assertion"; + expect(highestScoreFrom(words)).toEqual(correct) }); describe("in case of tied score", () => { From bb7fe8d4bdceb60df1a38dd7291a5301be5648d9 Mon Sep 17 00:00:00 2001 From: Anees Quateja Date: Tue, 26 Nov 2024 18:34:57 -0800 Subject: [PATCH 4/4] changes done --- 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 49bf9599..52ae8d23 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',