Skip to content
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"regenerator-runtime": "^0.12.1"
},
"dependencies": {
"core-js": "^2.6.6",
"core-js": "^2.6.10",
"vorpal": "^1.11.4"
}
}
21 changes: 21 additions & 0 deletions src/adagrams-test.js
Original file line number Diff line number Diff line change
@@ -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`)
}
}
81 changes: 80 additions & 1 deletion src/adagrams.js
Original file line number Diff line number Diff line change
@@ -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',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This data structure works, but it's not very DRY and would be tricky to change. For example, suppose I said you had the wrong number of "I"s - you would have to do a lot of counting to solve the problem.

Instead you could use your hash of letter frequencies to create this array dynamically.

'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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! nice work on this logic and clear comments describing what's going on.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider whether it would have been possible to do this check in the for loop above (had that not been a forEach loop)?

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot && wordArray.length <= 10)

score += 8
}

wordArray.forEach(letter => {
score += scoreChart[letter]
});
return score
}
}

// Do not remove this line or your tests will break!
export default Adagrams;