Skip to content

C22 Sphinx Somy C #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 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Writing Javascript functions with correct syntax, including:
- only use `const` and `let` variables
- use semi-colons where needed
- name variables and functions with camelCase
- name variables and functions with camelCase÷
- Practicing good git hygiene:
- make at least three small commits with meaningful commit messages
- Practicing TDD with JavaScript and the Jest testing framework
Expand All @@ -18,7 +18,7 @@ A test suite and sample game project is provided in order to practice TDD and ve

Note there are a handful of incomplete tests that currently throw exceptions. You should complete these tests and remove the exception.

## Getting Started
## Getting

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

Expand Down
100 changes: 96 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,107 @@
const letterPool = {
'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 letterValue = new Map([
[['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'], 1],
[['D', 'G'], 2],
[['B', 'C', 'M', 'P'], 3],
[['F', 'H', 'V', 'W', 'Y'], 4],
[['K'], 5],
[['J', 'X'], 8],
[['Q', 'Z'], 10]
]);
Comment on lines +8 to +16

Choose a reason for hiding this comment

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

Nice to see you using different constructors and getting familiar with them!



export const drawLetters = () => {
// Implement this method for wave 1

let lettersList = [];
for (const [letter, count] of Object.entries(letterPool)) {

Choose a reason for hiding this comment

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

Nice work finding the javascript version of .items!

for (let i = 0; i < count; i++) {
lettersList.push(letter);
}
}

let hand = [];
let temporaryLettersList = [...lettersList];

Choose a reason for hiding this comment

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

Nice, love to see the spread syntax being used!


for (let i = 0; i < 10; i++) {
const randomIndex = Math.floor(Math.random() * temporaryLettersList.length);
const randomLetter = temporaryLettersList.splice(randomIndex, 1)[0];

Choose a reason for hiding this comment

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

These are letter counts will the same size but what if they weren't? What if the input size varied? What would be the time complexity of your code? What CS fundamentals data structure did we learn about that is handy for keeping track of the number of occurrences?

hand.push(randomLetter);
}
return hand;
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
for (let i = 0; i < input.length; i++) {
const letter = input[i];

if (!lettersInHand.includes(letter)) {
return false;
}

const index = lettersInHand.indexOf(letter);
lettersInHand.splice(index, 1);

Choose a reason for hiding this comment

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

You are currently mutating a mutable object, if the caller of the function is not expecting this it could lead to some difficult to debug errors.

}
return true;
};


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

let totalScore = 0
for (let i = 0; i < word.length; i++) {
let letter = word[i].toUpperCase();

for (let [letters, score] of letterValue) {

Choose a reason for hiding this comment

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

Since you used a Map object you get access to this nice concise/readable syntax!

if (letters.includes(letter)) {
totalScore += score;
break;
}
}
}
if (word.length > 6){
totalScore += 8
}
return totalScore
Comment on lines +55 to +73

Choose a reason for hiding this comment

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

Great function all around!

};


export const highestScoreFrom = (words) => {
// Implement this method for wave 4

if (words.length === 0) {
return 0;
}

let winner = { word: '', score: 0 };

for (let word of words) {
let currentScore = scoreWord(word);

if (currentScore > winner.score) {
winner = { word, score: currentScore };
}

else if (currentScore === winner.score) {
if (word.length === 10 && winner.word.length !== 10) {
winner = { word, score: currentScore };
}

else if (
word.length !== 10 &&
winner.word.length !== 10 &&
word.length < winner.word.length
) {
winner = { word, score: currentScore };
}
}
}
Comment on lines +85 to +105

Choose a reason for hiding this comment

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

Your nested statements look good, easy to follow!

return winner;
};
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
});
Comment on lines +123 to +125

Choose a reason for hiding this comment

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

⭐️

});

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