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

Sharks - Jeannie #111

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
150 changes: 146 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,157 @@

const score_chart = {

Choose a reason for hiding this comment

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

oop careful! use camel case in JavaScript:

Suggested change
const score_chart = {
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
}
export const drawLetters = () => {
// Implement this method for wave 1
let 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
};
// create a string that holds all the avaliable letters - including probability
let playerHand = [];
let alphabet = "";
for (const [key, value] of Object.entries(LETTER_POOL)) {
alphabet += key.repeat(value);
}

// create 10 letters for the player hand
// subtract the letter from avaliable letters
while (playerHand.length < 10) {
let randomCharacter = alphabet[Math.floor(Math.random() * alphabet.length)];

if (LETTER_POOL[randomCharacter] === 0) {
continue;
} else {
playerHand.push(randomCharacter);
LETTER_POOL[randomCharacter] -= 1;
alphabet.replace(randomCharacter, '');

Choose a reason for hiding this comment

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

love this idea! One thing I would suggest is turning alphabet into an array instead of a string. That way when we change the index position's value to an empty string, we aren't creating a whole new copy of the data structure the way replace is doing.

Each time we call replace it has to make a new copy of the string.

}
}
return playerHand;
};


export const usesAvailableLetters = (input, lettersInHand) => {

Choose a reason for hiding this comment

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

👍 very interesting approach!

// Implement this method for wave 2
//function returns True if all letters in input are in lettersInHand

let listOfLetters = lettersInHand.slice();
let yourString = input.toUpperCase();
let boolSet = new Set();
//if letter is in lettersInHand, true is added to the set
for (const char of yourString) {
if (listOfLetters.includes(char)) {
boolSet.add(true);
const index = listOfLetters.indexOf(char);
listOfLetters.splice(index, 1);
// if letter is NOT in lettersInHand, false is added to the set
} else {
boolSet.add(false);
}
}

// determine if there is invalid letter or not by checking if false exist
if (boolSet.has(false)) {
return false;
} else {
return true
}
};



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

for (const letter of word) {
score += score_chart[letter.toUpperCase()]
} if (word.length >= 7) {
score += 8;
}
Comment on lines +117 to +119

Choose a reason for hiding this comment

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

I think standard practice is to have the if statement start on its own line. I only ever see a conditional on the same line as a closing curly brace is with else

Suggested change
} if (word.length >= 7) {
score += 8;
}
}
if (word.length >= 7) {
score += 8;
}

return score;
};


export const highestScoreFrom = (words) => {

Choose a reason for hiding this comment

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

👍

// Implement this method for wave 1

let score_list = [];
let max_score = 0;
let winning_word = "";
Comment on lines +126 to +128

Choose a reason for hiding this comment

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

Suggested change
let score_list = [];
let max_score = 0;
let winning_word = "";
let scoreList = [];
let maxScore = 0;
let winningWord = "";

for (let i = 0; i < words.length; i++) {
let score = scoreWord(words[i]);
let results = {};
results["score"] = score;
results["word"] = words[i];
score_list.push(results);

if (score > max_score) {
max_score = score;
winning_word = words[i];
}
}
let winner = { "word": winning_word, "score": max_score };

for (let item of score_list) {
if (item["score"] === max_score && item["word"].length === 10) {
winner = item;
return winner;
} else if (item["score"] === max_score && item["word"].length < winning_word.length) {

Choose a reason for hiding this comment

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

Suggested change
} else if (item["score"] === max_score && item["word"].length < winning_word.length) {
}
else if (item["score"] === max_score && item["word"].length < winning_word.length) {

winner = item;
}
}
return winner;
};





9 changes: 6 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ describe("Adagrams", () => {
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expectScores({
"": 0
});
});

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -133,7 +135,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 All @@ -145,7 +147,8 @@ describe("Adagrams", () => {
const words = ["XXX", "XXXX", "X", "XX"];
const correct = { word: "XXXX", score: scoreWord("XXXX") };

throw "Complete test by adding an assertion";
expect(highestScoreFrom(words)).toEqual(correct);

});

describe("in case of tied score", () => {
Expand Down