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

Diana S - Kunzite #140

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
148 changes: 146 additions & 2 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,159 @@
export const drawLetters = () => {
// Implement this method for wave 1
const RANGE_LOW = 0;
const RANGE_HIGH = 25

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 list_of_letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
const hand = [];

while (hand.length < 10) {
const random_number = Math.floor(Math.random() * (RANGE_HIGH - RANGE_LOW + 1)) + RANGE_LOW;
const letter = list_of_letters[random_number];
const letter_value = LETTER_POOL[letter];

if (hand.includes(letter)) {
const n = hand.filter(l => l === letter).length;
if (n < letter_value) {
hand.push(letter);
}
} else {
hand.push(letter);
}
}
console.log(hand);
return hand
};


export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
input = input.toUpperCase();
let isWordValid = true;

for (let i = 0; i < input.length; i++) {
const letter = input[i];

if (!lettersInHand.includes(letter)) {
isWordValid = false;
} else {
const v = lettersInHand.filter(l => l === letter).length;
const v2 = input.split(letter).length - 1;

if (v2 > v) {
isWordValid = false;
}
}
}
console.log(isWordValid);
return isWordValid;
};


export const scoreWord = (word) => {
// Implement this method for wave 3
if (word.length === 0) {
return 0;
}

const scoreChart = {
'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
};

word = word.toUpperCase();
let scoreSum = 0;

for (let i = 0; i < word.length; i++) {
const letter = word[i];
const value = scoreChart[letter];
scoreSum += value;
}

if (word.length >= 7 && word.length <= 10) {
scoreSum += 8;
}
console.log(word.length);
return scoreSum;
};


export const highestScoreFrom = (words) => {
// Implement this method for wave 4
let winner = {
word: "",
score: 0
};

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

if (score > winner.score) {
winner.word = word;
winner.score = score;
} else if (score === winner.score) {
if (word.length === 10 && winner.word.length !== 10) {
winner.word = word;
winner.score = score;
} else if (word.length < winner.word.length && winner.word.length !== 10) {
winner.word = word;
winner.score = score;
}
}
}

return winner;
};

2 changes: 1 addition & 1 deletion test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("Adagrams", () => {
});
});

describe.skip("highestScoreFrom", () => {
describe("highestScoreFrom", () => {
it("returns a hash that contains the word and score of best word in an array", () => {
const words = ["X", "XX", "XXX", "XXXX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };
Expand Down