-
Notifications
You must be signed in to change notification settings - Fork 169
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
base: main
Are you sure you want to change the base?
Changes from all commits
c714252
f73b90b
dd75c26
5d78395
81de543
c436611
48e28ff
64a69da
ded4cdb
bf50f02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}/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 = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ describe("Adagrams", () => { | |
}); | ||
|
||
it("returns a score of 0 if given an empty input", () => { | ||
throw "Complete test"; | ||
expect(scoreWord("")).toBe(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", () => { | ||
|
@@ -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") }; | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
}); | ||
|
||
describe("in case of tied score", () => { | ||
|
There was a problem hiding this comment.
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.