Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,104 @@
const Adagrams = {
drawLetters() {
// Implement this method for wave 1
// eslint-disable-next-line max-len
const letters = ['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', 'M', 'M', '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'];

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 might store a hash of letter frequencies like this

letterQuantities: {
  A: 9, B: 2, ...
}

and use it to build a pool of letters dynamically.


const shuffleLetters = function(array) {
//used online resources to piece this together
let i = array.length - 1;
while (i > 0) {
const j = Math.floor(Math.random() * i);
const temp = array[i];
array[i] = array[j];
array[j] = temp;
i--;
}
return array;
};

const randomOrder = shuffleLetters(letters)

Choose a reason for hiding this comment

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

Here, you shuffle the entire array pool of letters... and then, you take the first ten. I think you could refactor this so that you randomly pick ten letters, instead of shuffling the entire array.


const createHand = function(array) {
const drawn = []
while (drawn.length < 10) {
const letter = array[0]
drawn.push(letter);
array.shift();
}
return drawn;
};
return(createHand(randomOrder));

},


usesAvailableLetters(input, lettersInHand) {
const hand = {}
let outcome = true
lettersInHand.forEach(element => {
if (hand[element]) {
hand[element] += 1;
} else {
hand[element] = 1;
}
});

const letters = input.split('')

letters.forEach(char => {

Choose a reason for hiding this comment

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

Consider using a loop that allows you to break out so that as soon as you encounter a false you exit the loop.

if (hand[char] < 0) {
outcome = false;
} else if (hand[char]) {
hand[char] -= 1;
} else {
outcome = false;
}
});
return outcome;

},

scoreWord(input) {
const letterPoints = {

Choose a reason for hiding this comment

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

style note: move this object to ahead of all the functions, but inside the module.

'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
}

let total = 0
const word = input.toUpperCase()

if (word.length > 6) {total += 8}

const letters = word.split('')
letters.forEach(char => {
total += letterPoints[char]
});
return total
}
};

// Do not remove this line or your tests will break!
Expand Down