Skip to content
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

Kunzite - Allie S. #82

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ Note there are a handful of incomplete tests that currently throw exceptions. Yo

After forking and cloning this repo you should `cd` to the project directory and run:

```bash
$ brew install yarn
```
```bash
$ yarn install
```

```bash
$ yarn add
```

```bash
$ yarn add --dev eslint eslint-plugin-jest
```

Similar to using virtual environments in Python projects, this makes the yarn package manager download and install any dependencies for the project (such as Jest).

### When I `yarn add`, I Get a Lot of Text: What do I do?!
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.8.0",
"babel-plugin-module-resolver": "^3.2.0",
"eslint": "^8.41.0",
"eslint-plugin-jest": "^27.2.1",
"jest": "^24.8.0",
"regenerator-runtime": "^0.12.1"
},
Expand Down
134 changes: 130 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,141 @@
const LETTER_POOL = {
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 SCORE_CHART = {
AEIOULNRST: 1,
DG: 2,
BCMP: 3,
FHVWY: 4,
K: 5,
JX: 8,
QZ: 10,
};

export const drawLetters = () => {
// Implement this method for wave 1
// Implement this method for wave 1
const LETTER_POOL_COPY = {
...LETTER_POOL,
};
// Creates array to hold 10 letters
let handArray = [];
// letterKeys will hold an array of all of the available letters
let letterKeys = Object.keys(LETTER_POOL_COPY);

for (let i = 0; i < 10; i++) {
// Get random letter from letterKeys array and add to handArray
let randomIndex = Math.floor(Math.random() * letterKeys.length);
let randomLetter = letterKeys[randomIndex];
handArray.push(randomLetter);

// Decrease the count of letter from hash map LETTER_POOL_COPY
LETTER_POOL_COPY[randomLetter]--;

// Checks if letter count is 0
if (LETTER_POOL_COPY[randomLetter] === 0) {
// Delete letter key from hash map and remove letter from letterKeys
delete LETTER_POOL_COPY[randomLetter];
letterKeys.splice(randomIndex, 1);
}
}
return handArray;
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
// Implement this method for wave 2

// Makes a copy array of lettersInHand
let lettersInHandCOPY = [...lettersInHand];

// Iterates over the input string
for (const letter of input) {
// Checks if each letter is valid against the draw letters
if (lettersInHandCOPY.includes(letter)) {
let letterIndex = lettersInHandCOPY.indexOf(letter);
lettersInHandCOPY.splice(letterIndex, 1);
} else {
return false;
}
}
return true;
};

export const scoreWord = (word) => {
// Implement this method for wave 3
let score = 0;

// Iterating through word and SCORE_CHART, calculates score
for (const letter of word.toUpperCase()) {
for (const [keys, value] of Object.entries(SCORE_CHART)) {
if (keys.includes(letter)) {
score += value;
}
}
}
// Checks if the length of the word is between 7-10, adding 8 bonus points
const wordLength = word.length;
if (wordLength >= 7 && wordLength <= 10) {
score += 8;
}

return score;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
// Implement this method for wave 4
let bestWord = '';
let highestScore = 0;

for (let i = 0; i < words.length; i++) {
const currentWord = words[i];
const currentWordScore = scoreWord(currentWord);

if (currentWordScore > highestScore) {
bestWord = currentWord;
highestScore = currentWordScore;
} else if (currentWordScore === highestScore) {
if (currentWord.length === 10 && bestWord.length === 10) {
bestWord = bestWord;
highestScore = highestScore;
} else if (currentWord.length === 10 && bestWord.length !== 10) {
bestWord = currentWord;
highestScore = currentWordScore;
} else if (currentWord.length !== 10 && bestWord.length === 10) {
bestWord = bestWord;
highestScore = highestScore;
} else if (currentWord.length > bestWord.length) {
bestWord = bestWord;
highestScore = highestScore;
} else if (currentWord.length < bestWord.length) {
bestWord = currentWord;
highestScore = currentWordScore;
}
}
}

return { word: bestWord, score: highestScore };
};
Loading