-
Notifications
You must be signed in to change notification settings - Fork 41
Kayla Ecker - Caret #44
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: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -1,14 +1,129 @@ | ||
| const scrabbleLetters = { | ||
| 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, | ||
| }; | ||
|
|
||
| const Scrabble = { | ||
| score: function(word) { | ||
| // TODO: implement score | ||
| } | ||
| score: function(word) { | ||
|
|
||
| let regex = /^[a-zA-Z]+$/; | ||
|
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. Making variables you never reassign |
||
|
|
||
| if (!regex.test(word)) { | ||
| throw new Error('Invalid characters'); | ||
| } | ||
|
|
||
| if (word.length > 7) { | ||
| throw new Error('Word must be 1 - 7 characters long'); | ||
| } | ||
|
|
||
| let lowercaseWord = word.toLowerCase(); | ||
| let score = 0; | ||
|
|
||
|
|
||
| for (let i = 0; i < lowercaseWord.length; i++) { | ||
|
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. This works fine, you can also use |
||
| score += scrabbleLetters[lowercaseWord[i]]; | ||
| } | ||
|
|
||
| if (lowercaseWord.length === 7) { | ||
| score += 50; | ||
| } | ||
|
|
||
| return score; | ||
| }, | ||
|
|
||
| highestScoreFrom: function(wordsArray) { | ||
|
|
||
| if (!Array.isArray(wordsArray) || wordsArray.length === 0 ) { | ||
| throw new Error('Invalid'); | ||
| } | ||
|
|
||
| if (wordsArray.length === 1) { | ||
| return wordsArray[0]; | ||
| } | ||
|
|
||
| let max = 0; | ||
| let highestWord = ''; | ||
|
|
||
| for (let i = 0; i < wordsArray.length; i++) { | ||
| let score = this.score(wordsArray[i]); | ||
|
|
||
| if (score > max) { | ||
| max = score; | ||
| highestWord = wordsArray[i]; | ||
| } | ||
| else if (score === max) { | ||
| if (highestWord.length !== 7 && | ||
| (wordsArray[i].length === 7 || (wordsArray[i].length < highestWord.length))) { | ||
| highestWord = wordsArray[i]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return highestWord; | ||
| }, | ||
|
|
||
|
|
||
| }; | ||
|
|
||
| Scrabble.Player = class { | ||
| constructor(name) { | ||
| if (name === undefined) { | ||
| throw new Error('Invalid - must be a name'); | ||
|
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. Good work creating an object to throw the error. |
||
| } | ||
| this.name = name; | ||
| this.playArray = []; | ||
| this.plays = this.playArray; | ||
| } | ||
|
|
||
| play(word) { | ||
| let regex = /^[a-zA-Z]+$/; | ||
|
|
||
| if (word === undefined || !regex.test(word)) { | ||
|
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 use of Regex |
||
| throw new Error('Invalid word'); | ||
| } | ||
|
|
||
| if (! this.hasWon()) { | ||
| if (this.playArray.push(word)) { | ||
| return true; | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| totalScore() { | ||
| let total = 0; | ||
|
|
||
| for (let i = 0; i < this.plays.length; i++) { | ||
| total += Scrabble.score(this.plays[i]); | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| hasWon() { | ||
|
|
||
| if (this.totalScore() >= 100) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| highestScoringWord() { | ||
| let example = Scrabble.highestScoreFrom(this.plays); | ||
| return example; | ||
| } | ||
|
|
||
| // TODO: add the highestScoreFrom method | ||
| highestWordScore() { | ||
| let highestWord = this.highestScoringWord(); | ||
| return Scrabble.score(highestWord); | ||
| } | ||
| }; | ||
|
|
||
| }; | ||
|
|
||
| Scrabble.Player = class { | ||
| // TODO: implement the Player class | ||
| }; | ||
|
|
||
| module.exports = Scrabble; | ||
| module.exports = Scrabble; | ||
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 could be written as:
score(word) {