Skip to content

Final version of JS adagrams-Diana S. #133

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 10 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.

Choose a reason for hiding this comment

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

This will probably break some aspects of VSCode. JSON format does not support comments like in JavaScript.

// 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}/src/adagrams.js"
}
]
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "jest",
"coverage": "open coverage/lcov-report/index.html",
"demo-game": "babel-node src/demo.js"
"demo-game": "babel-node src/demo.js",
"adagrams": "babel-node src/adagrams.js"
},
"repository": {
"type": "git",
Expand All @@ -27,6 +28,8 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^24.8.0",
"babel-plugin-module-resolver": "^3.2.0",
"eslint": "^8.28.0",
"eslint-plugin-jest": "^27.1.6",
"jest": "^24.8.0",
"regenerator-runtime": "^0.12.1"
},
Expand Down
139 changes: 135 additions & 4 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,146 @@
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 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
const thisHand = [];

Choose a reason for hiding this comment

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

Nice job with this function!


const letterDist = Object.entries(letterPool);

const bigLists = [];
for (const [letter, value] of letterDist) {
for (let i = 0; i < value; i++) {
bigLists.push(letter);
}
}

while (thisHand.length < 10) {
const index = Math.floor(Math.random() * bigLists.length);
thisHand.push(bigLists[index]);
bigLists.splice(index, 1);
}
return thisHand;
};

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

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

const wordUp = word.toUpperCase();

if (wordUp.length === 0) {
return scoreTotal;
}

let lettersFromWord = wordUp.split("");

for (let i = 0; i < lettersFromWord.length; i++) {
let letter = lettersFromWord[i];
scoreTotal += scoreChart[letter];
}

if (lettersFromWord.length >= 7 && lettersFromWord.length <= 10) {
scoreTotal += extraPoints;
}

return scoreTotal;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
// const words = ["XXX", "XXXX", "X", "XX"];
let highScore = {
score: scoreWord(words[0]),
word: words[0],
};

for (let i = 1; i < words.length; i++) {
let currentWord = words[i];
let currentScore = scoreWord(currentWord);

if (highScore.score < currentScore) {
highScore.score = currentScore;
highScore.word = currentWord;
} else if (highScore.score === currentScore) {
// if (highScore.word.length === currentScore.length) {
// continue;
// }
if (currentWord.length === 10 && highScore.word.length != 10) {
highScore.word = currentWord;
} else if (
currentWord.length < highScore.word.length &&
highScore.word.length != 10
) {
highScore.word = currentWord;
} else {
continue;
}
}
}
return highScore;
};
6 changes: 3 additions & 3 deletions test/adagrams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Adagrams", () => {
});

it("returns a score of 0 if given an empty input", () => {
throw "Complete test";
expect(scoreWord("")).toBe(0);

Choose a reason for hiding this comment

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

Great 👍🏽

});

it("adds an extra 8 points if word is 7 or more characters long", () => {
Expand All @@ -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 All @@ -145,7 +145,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);

Choose a reason for hiding this comment

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

Nice!

});

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