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

Sea Turtles Carranza, Katina #114

Open
wants to merge 5 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
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// 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",
"name": "vscode-jest-tests.v2",
"request": "launch",
"args": [
"--runInBand",
"--watchAll=false",
"--testNamePattern",
"${jest.testNamePattern}",
"--runTestsByPath",
"${jest.testFile}"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"program": "${workspaceFolder}/node_modules/.bin/jest",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
]
}
99 changes: 97 additions & 2 deletions src/adagrams.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,110 @@
const availableLetters = {
'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 = () => {
Copy link

Choose a reason for hiding this comment

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

I would use the spread operator to make a copy of the letterPool
use a for loop to draw 10 letters
use math.floor to make the random draw
push the letter to letterHand
return letterHand

// Implement this method for wave 1
let letterHand = [];
while (letterHand.length < 10){
// picked_letter = Math.floor(Math.random(Object.keys(available_letters)));
const randomIndex = Math.floor(Math.random()*26);
const letterList = Object.keys(availableLetters);
const picked_letter=letterList[randomIndex];
const elementCount = letterHand.reduce((total,x) => (x==picked_letter ? total+1 : total) ,0);
if (elementCount < availableLetters[picked_letter][0]){
letterHand.push(picked_letter);
}
}
return letterHand
};

export const usesAvailableLetters = (input, lettersInHand) => {
export const usesAvailableLetters = (input,lettersInHand) => {
// Implement this method for wave 2
const lettersInHandCopy = [...lettersInHand];
let result = true;
// const word = input.toUpperCase()
for (const char of input){
if (lettersInHandCopy.includes(char)===true){
const index = lettersInHandCopy.indexOf(char);
lettersInHandCopy.splice(index,1);
result = true;
} else {
result = false;
return result;
}
}
return result
};

export const scoreWord = (word) => {
// Implement this method for wave 3
let score = 0;
if (word.length == 0){
score = 0;
return score;
}
for (const letter of word.toUpperCase()){
score += availableLetters[letter][1];
}
if (word.length >= 7 && word.length <= 10) {
score += 8;
}
return score
};

export const highestScoreFrom = (words) => {
// Implement this method for wave 1
};
let scores = []
let topScoringWords = []
let lgth = 0;
let longestWord;

for (let word of words){
let wordScore = scoreWord(word);
scores.push(wordScore);
// Loop to find the longest word
if (word.length > lgth){
lgth = word.length;
longestWord = word;
}
}
let maxScore = Math.max(...scores);
// makes list of all words with the top score
for (let i = 0; i < scores.length; i++){
if (scores[i] === maxScore){
topScoringWords.push(words[i]);
}
}
if (longestWord.length === 10){
return {"score": maxScore,"word":longestWord}
} else {
// finds shortest word with top score
let topWord = topScoringWords.reduce((a,b) => a.length <= b.length ? a:b);
return {"score": maxScore, "word":topWord}
}
};
9 changes: 5 additions & 4 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 @@ -144,8 +146,7 @@ describe("Adagrams", () => {
it("accurately finds best scoring word even if not sorted", () => {
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