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

Amethyst - AnBa - Adagram JS #144

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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/test/adagrams.test.js"
}
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "JavaScript version of the Adagrams project",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --mode development",
"test": "jest",
"coverage": "open coverage/lcov-report/index.html",
"demo-game": "babel-node src/demo.js"
Expand Down
102 changes: 97 additions & 5 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,107 @@
// global variable
const LETTER_POOL = {
A: 9,
B: 2,
C: 2,
D: 4,
E: 12,
F: 2,
Comment on lines +1 to +8

Choose a reason for hiding this comment

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

Great job creating this global variable outside of each function so each function can access it & we can also keep the program DRY 😄

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
}
// the following line of code imports the library lodash
// we use this library because we want to use the method sampleSize and the method cloneDeep
const _ = require('lodash');

export const drawLetters = () => {
// Implement this method for wave 1
let letterList = [];
for (let [letter, freq] of Object.entries(LETTER_POOL)) {
letterList.push(...Array(freq).fill(letter));
}
Comment on lines 34 to +38

Choose a reason for hiding this comment

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

Great work getting comfortable with JavaScript's syntax. It looks like you're getting use to looping over data and adding conditional logic ✅

return _.sampleSize(letterList, 10);
};

export const usesAvailableLetters = (input, lettersInHand) => {
// Implement this method for wave 2
const lettersInHand_deepcopy = _.cloneDeep(lettersInHand);
for (let letter of input.toUpperCase()) {
if (lettersInHand_deepcopy.includes(letter)) {
lettersInHand_deepcopy.splice(lettersInHand_deepcopy.indexOf(letter), 1);
} else {
return false;
}
}
return true;
};

export const scoreWord = (word) => {
// Implement this method for wave 3
// we are using Map instead of Object because the keys in Object must be string, symbol or number
const valuesMap = 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 +56 to +64

Choose a reason for hiding this comment

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

Excellent choice with this data structure!


let wordValue = 0;
for (let letter of word) {
let letterValue = 0;
for (let lettersArray of valuesMap.keys()) {
if (lettersArray.includes(letter.toUpperCase())) {
letterValue = valuesMap.get(lettersArray);
}
}
// we can find letterValue with just 1 line of code
// but I prefer the previous approach because of its readability
// let letterValue = [...valuesMap].find(([key, value]) => key.includes(letter.toUpperCase()))[1];
wordValue += letterValue;
}

if (word.length >= 7 && word.length <= 10) {
wordValue += 8;
Comment on lines +77 to +81

Choose a reason for hiding this comment

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

Nice job translating this logic 👍🏾

}
return wordValue;
};

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

// iterate through the array of words and choose the word with highest score
// in case of tied score, we compare the words'length and determine which word will be chosen
words.forEach(word => {
const currentScore = scoreWord(word);

if (currentScore > result.score) {
result.word = word;
result.score = currentScore;
} else if (currentScore === result.score) {
if (result.word.length !== 10 && (word.length === 10 || word.length < result.word.length)) {
result.word = word;
Comment on lines +94 to +102

Choose a reason for hiding this comment

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

Hi An Ba! Great job completing JS Adagrams! Congrats on finishing your first JavaScript project 🎉 Excellent choice with this scoring approach

}
}
});
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
Loading