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

C19 Kunzite - Abby Castillo #69

Open
wants to merge 17 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
122 changes: 119 additions & 3 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,131 @@
export const drawLetters = () => {
// Implement this method for wave 1
// initialize object to hold letter frequency table
const letterFreqs = {

Choose a reason for hiding this comment

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

Nothing wrong with it being defined here but because mappings like this can be valuable outside of being used in functions, it's often a better idea to define them in the module's main scope so that they can be exported and used elsewhere and in other functions in the same module!

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,
};

// fill letterPool array with all the letters
const letterPool = [];

for (const [key, value] of Object.entries(letterFreqs)) {

Choose a reason for hiding this comment

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

Nice job with this one, creating an array to hold all of the possible letter combinations with respect to their distribution!

for (let i = 1; i < value + 1; i++) {
letterPool.push(key);
}
}

// draw letters into hand array
let hand = [];

Choose a reason for hiding this comment

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

Since hand isn't reassigned, it should be a const.


for (let i = 0; i < 10; i++) {
let selector = Math.random() * letterPool.length;

let letter = letterPool.splice(selector, 1)[0];

Choose a reason for hiding this comment

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

nice job doubling up the array mutation and accessing the letter from the .splice() return value.


hand.push(letter);
}

return hand;
};

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

Choose a reason for hiding this comment

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

Not sure if you were trying to make a shallow copy here in an effort to avoid mutation, but just note that any mutation applied to handCopy will also apply to lettersInHand. If you want a shallow copy, you can instead use the spread operator:

const handCopy = [...lettersInHand];
handCopy.pop()
// now both handCopy and lettersInHand would be missing their last element.

Also, handCopy should be a const


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

if (handCopy.includes(letter)) {
let index = handCopy.indexOf(letter);
handCopy.splice(index, 1);
} else {
return false;
}
}

return true;
};

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

const scoreChart = {
1: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2: ["D", "G"],
3: ["B", "C", "M", "P"],
4: ["F", "H", "V", "W", "Y"],
5: ["K"],
8: ["J", "X"],
10: ["Q", "Z"],
};

let score = 0;

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

for (let i = 0; i < word.length; i++) {
let letter = word[i];
for (const [key, value] of Object.entries(scoreChart)) {
if (value.includes(letter.toUpperCase())) {
score += parseInt(key);
// console.log(score);
}
}
}

return score;
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 4
let largestWord = "";
let largestWordScore = 0;

for (let i = 0; i < words.length; i++) {
let word = words[i];
let wordScore = scoreWord(word);
if (wordScore > largestWordScore) {
largestWord = word;
largestWordScore = wordScore;
} else if (wordScore === largestWordScore) {
if (largestWord.length === 10) {
largestWord = largestWord;
} else if (word.length === 10) {
largestWord = word;
} else if (word.length < largestWord.length) {
largestWord = word;
} else if (largestWord.length < word.length) {
largestWord = largestWord;
}
}
}
let result = { word: largestWord, score: largestWordScore };

Choose a reason for hiding this comment

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

Since result isn't reassigned, it should be a const.

return result;
};
6 changes: 3 additions & 3 deletions src/demo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Model from 'demo/model';
import View from 'demo/view';
import Controller from 'demo/controller';
import Model from "demo/model";
import View from "demo/view";
import Controller from "demo/controller";

// Initialize the controller I guess
const game = new Controller(Model, View);
Expand Down
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