diff --git a/package-lock.json b/package-lock.json index abd4c8ad..f83fcf45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2416,9 +2416,9 @@ "dev": true }, "core-js": { - "version": "2.6.6", - "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.6.tgz", - "integrity": "sha512-Mt/LaAym54NXnrjEMdo918cT2h70tqb/Yl7T3uPHQHRm5SxVoqlKmerUy4mL11k8saSBDWQ7ULIHxmeFyT3pfg==" + "version": "2.6.10", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.10.tgz", + "integrity": "sha512-I39t74+4t+zau64EN1fE5v2W31Adtc/REhzWN+gWRRXg6WH5qAsZm62DHpQ1+Yhe4047T55jvzz7MUqF/dBBlA==" }, "core-js-compat": { "version": "3.1.0", diff --git a/package.json b/package.json index 002696e1..de8447e6 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "regenerator-runtime": "^0.12.1" }, "dependencies": { - "core-js": "^2.6.6", + "core-js": "^2.6.10", "vorpal": "^1.11.4" } } diff --git a/src/adagrams-test.js b/src/adagrams-test.js new file mode 100644 index 00000000..2483dbe6 --- /dev/null +++ b/src/adagrams-test.js @@ -0,0 +1,21 @@ +let inputArray = [ 'J', 'I', 'L', 'T' ] +let handHash = { L: 2, T: 1, I: 2, N: 1, O: 1, J: 1, D: 2 } + + +inputArray.forEach(letter => { + if (handHash[letter] == null) { + return false; + } else { + handHash[letter] -= 1; + } +}); + +console.log(handHash) + +for (let index = 0; index < inputArray.length; index++) { + if (handHash[inputArray[index]] < 0 || handHash[inputArray[index]] == null) { + console.log(`${inputArray[index]} is false`) + } else { + console.log(`${inputArray[index]} is true`) + } +} \ No newline at end of file diff --git a/src/adagrams.js b/src/adagrams.js index 54043451..e58dcdde 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,8 +1,87 @@ const Adagrams = { drawLetters() { // Implement this method for wave 1 + const letterPoolHash = {'A':9, 'B':2, 'C':2, 'D':4, 'E':12, 'F':2, 'G':3, 'H':2, 'I':9, 'J':1, 'K':1, 'L':4, '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 letterPoolArray = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', + 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', + 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'F', 'F', 'G', 'G', + 'G', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', + 'I', 'J', 'K', 'L', 'L', 'L', 'L', 'N', 'N', 'N', 'N', + 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'P', + 'P', 'Q', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'S', 'S', + 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'U', 'U', 'U', 'U', + 'V', 'V', 'W', 'W', 'X', 'Y', 'Y', 'Z']; + + let lettersInHand = []; + + // turn draw random letter into an anonymous function? or a helper function? + + // continue this loop until there are ten letters in the player hand + for (let index = 0; lettersInHand.length < 10; index++) { + // code for random courtesy of https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array + // pull a random letter from the letter pool array + let randomLetter = letterPoolArray[Math.floor(Math.random() * letterPoolArray.length)]; + // condition: if adding the letter to the player hand would exceed the count of letters in the pool, redraw + let count = lettersInHand.reduce((total, letter) => total + (letter === randomLetter), 0); + if (count == letterPoolHash[randomLetter]) { + randomLetter = letterPoolArray[Math.floor(Math.random() * letterPoolArray.length)]; + } else { // add the letter to player hand array + lettersInHand.push(randomLetter) + } + } + return lettersInHand }, -}; + + usesAvailableLetters(input, lettersInHand) { + + // turn hand array into a hash map that we can compare to input + let handHash = {}; + + lettersInHand.forEach(letter => { + if (handHash[letter] == null) { + handHash[letter] = 1 + } else { + handHash[letter] += 1 + } + }); + + let inputArray = input.toUpperCase().split('') + + // remove input letters from handHash. If letter is not in hash, return false. + inputArray.forEach(letter => { + if (handHash[letter] == null) { + return false; + } else { + handHash[letter] -= 1; + } + }); + + // loop over handHash: if letter count in input is ever greater than letter count in hash, return false. Else return true. + for (let index = 0; index < inputArray.length; index++) { + if (handHash[inputArray[index]] < 0 || handHash[inputArray[index]] == null) { + return false; + }} + return true; + }, + + scoreWord(word) { + + 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} + + let wordArray = word.toUpperCase().split('') + let score = 0 + + if (wordArray.length >= 7) { + score += 8 + } + + wordArray.forEach(letter => { + score += scoreChart[letter] + }); + return score + } +} // Do not remove this line or your tests will break! export default Adagrams;