Skip to content

Leaves - Yitgop #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
123 changes: 120 additions & 3 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,125 @@
// 1. Set up array data structure:
const pool = Array(9).fill('A').concat(

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.

Array(2).fill('B'),
Array(2).fill('C'),
Array(4).fill('D'),
Array(12).fill('E'),
Array(2).fill('F'),
Array(3).fill('G'),
Array(2).fill('H'),
Array(9).fill('I'),
Array(1).fill('J'),
Array(1).fill('K'),
Array(4).fill('L'),
Array(2).fill('M'),
Array(6).fill('N'),
Array(8).fill('O'),
Array(2).fill('P'),
Array(1).fill('Q'),
Array(6).fill('R'),
Array(4).fill('S'),
Array(6).fill('T'),
Array(4).fill('U'),
Array(2).fill('V'),
Array(2).fill('W'),
Array(1).fill('X'),
Array(2).fill('Y'),
Array(1).fill('Z')
);
let POOL = pool;

const Adagrams = {

drawLetters() {
// Implement this method for wave 1
let i = 0;
const lettersInHand = new Array(10);
while (i < 10) {

Choose a reason for hiding this comment

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

There's a small bug here. Consider how you could make sure that you don't choose the same letter 10 times (however unlikely)

lettersInHand[i] = POOL[Math.floor(Math.random() * POOL.length)];
i += 1;
}
return lettersInHand;
},

usesAvailableLetters(input, lettersInHand) {
const lettersObj = new Object();
lettersInHand.forEach(function(element){
if (lettersObj[element]) {
lettersObj[element] += 1;
} else {
lettersObj[element] = 1;
}
});

let j = 0;
while (j < input.length) {
if (lettersObj[input.charAt(j)]) {
lettersObj[input.charAt(j)] -= 1;
} else {
return false;
}
j += 1;
}

let n = 0;
while (n < lettersObj.length) {
if (lettersObj[j] < 0) {
return false;
}
n += 1;
}
return true
},

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

scoreWord(word) {
const final_word = word.toUpperCase();
let score = 0;
let m = 0;
while (m < final_word.length) {
score += this.scoreChart[final_word.charAt(m)];
m += 1;
}

switch (final_word.length) {
case 7:

Choose a reason for hiding this comment

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

Consider how you could refactor to DRY up this code.

case 8:
case 9:
case 10:
score += 8;
break;
}

return score
},
};
}

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