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 - Morgan B. #93

Open
wants to merge 9 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
package.json.lock
.DS_Store
coverage
coverage
.vscode
124 changes: 124 additions & 0 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,139 @@
const letterFreqAndScore = {
A: [9, 1],
B: [2, 3],
C: [2, 3],
D: [4, 2],
E: [12, 1],
F: [2, 4],
G: [3, 2],
H: [2, 4],
I: [9, 1],
J: [1, 8],
K: [1, 5],
L: [4, 1],
M: [2, 3],
N: [6, 1],
O: [8, 1],
P: [2, 3],
Q: [1, 10],
R: [6, 1],
S: [4, 1],
T: [6, 1],
U: [4, 1],
V: [2, 4],
W: [2, 4],
X: [1, 8],
Y: [2, 4],
Z: [1, 10],
};

export const drawLetters = () => {
// Implement this method for wave 1
let letterList = [];
let returnedLetters = [];

for (const [letter, number] of Object.entries(letterFreqAndScore)) {
for (let i = 0; i < number[0]; i++) {
letterList.push(letter);
}
}

while (returnedLetters.length < 10) {
let currentLetter =
letterList[Math.floor(Math.random() * letterList.length)];
returnedLetters.push(currentLetter);
for (let i = 0; i < letterList.length; i++) {
if (letterList[i] === currentLetter) {
letterList.splice(i, 1);
{
break;
}
}
}
}
Comment on lines +41 to +53

Choose a reason for hiding this comment

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

Suggested change
while (returnedLetters.length < 10) {
let currentLetter =
letterList[Math.floor(Math.random() * letterList.length)];
returnedLetters.push(currentLetter);
for (let i = 0; i < letterList.length; i++) {
if (letterList[i] === currentLetter) {
letterList.splice(i, 1);
{
break;
}
}
}
}
while (returnedLetters.length < 10) {
const letterIndex = Math.floor(Math.random() * letterList.length);
const currentLetter = letterList[letterIndex];
returnedLetters.push(currentLetter);
letterList.splice(letterIndex, 1);
}
return returnedLetters;


return returnedLetters;
};

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

for (const letter of lettersInHand) {
if (letterDict.hasOwnProperty(letter.toUpperCase())) {
letterDict[letter] += 1;
} else {
letterDict[letter] = 1;
}
}

for (const letter of input) {
if (letterDict[letter] > 0) {
letterDict[letter] -= 1;
} else {
return false;
}
}

return true;
Comment on lines +60 to +78

Choose a reason for hiding this comment

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

👍

};

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

for (const letter of word) {
score += letterFreqAndScore[letter.toUpperCase()][1];

Choose a reason for hiding this comment

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

How would you handle input that has characters that are not in letterFreqAndScore, for example what if word was "cat!" ?

When your for loop gets to "!" then letterFreqAndScore['!'] will return undefined. When we add undefined to a number like total then the result will be NaN, which introduces a bug.

You could have a conditional check before adding score to total, by checking that the letter is a key in letterScore.

}
if (word.length > 6) {
score += 8;
}
Comment on lines +88 to +90

Choose a reason for hiding this comment

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

You might also see this written with a ternary operator (on line 83):

let score = word.length >= 7 ? 8 : 0;


return score;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 1
let scores = [];
let lowest = [];
const result = {
word: null,
score: null,
};

for (const word of words) {
let currentWordScore = scoreWord(word);

if (scores.length === 0) {
scores.push([word, currentWordScore]);
} else if (currentWordScore > scores[0][1]) {
scores = [[word, currentWordScore]];
} else if (currentWordScore === scores[0][1]) {
scores.push([word, currentWordScore]);
}
}

if (scores.length === 1) {
result["word"] = scores[0][0];
result["score"] = scores[0][1];
return result;
}

for (const [word, score] of scores) {
let wordLength = word.length;

if (wordLength === 10) {
result["word"] = word;
result["score"] = score;
return result;
} else if (lowest.length === 0) {
lowest = [word, score, wordLength];
} else if (wordLength < lowest[2]) {
lowest = [word, score, wordLength];
}
}

result["word"] = lowest[0];
result["score"] = lowest[1];
return result;
};
8 changes: 5 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,7 @@ 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