-
Notifications
You must be signed in to change notification settings - Fork 41
Sara Chandler -- Carets #30
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,149 @@ | ||
|
|
||
| const Scrabble = { | ||
| score: function(word) { | ||
| // TODO: implement score | ||
| } | ||
| if (!word.match(/^([a-zA-Z]){1,7}$/)) { | ||
| throw new Error('Not a valid word!'); | ||
| } else { | ||
| word = word.toUpperCase(); | ||
| } | ||
|
|
||
| let score = 0; | ||
|
|
||
| if (word.length === 7) { | ||
| score += 50; | ||
| } else { | ||
| score = 0; | ||
| } | ||
|
|
||
| let wordArr = word.split(""); | ||
|
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. Since you never reassign |
||
|
|
||
|
|
||
| wordArr.forEach((letter) => { | ||
| switch (letter) { | ||
| case 'A': | ||
| case 'E': | ||
| case 'I': | ||
| case 'O': | ||
| case 'U': | ||
| case 'L': | ||
| case 'N': | ||
| case 'R': | ||
| case 'S': | ||
| case 'T': | ||
| score += 1; | ||
| break; | ||
|
|
||
| case 'D': | ||
| case 'G': | ||
| score += 2; | ||
| break; | ||
|
|
||
| case 'B': | ||
| case 'C': | ||
| case 'M': | ||
| case 'P': | ||
| score += 3; | ||
| break; | ||
|
|
||
| case 'F': | ||
| case 'H': | ||
| case 'V': | ||
| case 'W': | ||
| case 'Y': | ||
| score += 4; | ||
| break; | ||
|
|
||
| case 'k': | ||
| score += 5; | ||
| break; | ||
|
|
||
| case 'J': | ||
| case 'X': | ||
| score += 8; | ||
| break; | ||
|
|
||
| case 'Q': | ||
| case 'Z': | ||
| score += 10; | ||
| break; | ||
|
|
||
| default: | ||
| throw new Error('Not a valid letter!'); | ||
| } | ||
| }); | ||
| return score; | ||
| }, | ||
|
|
||
| // TODO: add the highestScoreFrom method | ||
| highestScoreFrom: function(arrayOfWords) { | ||
| if (arrayOfWords.length === 0) { | ||
| throw new Error('No words...') | ||
| } | ||
|
|
||
| let currentWinningWord = arrayOfWords[0]; | ||
| let currentWinningScore = this.score(currentWinningWord); | ||
|
|
||
| arrayOfWords.forEach((word) => { | ||
| let wordScore = this.score(word); | ||
| if (currentWinningScore < wordScore) { | ||
| currentWinningWord = word; | ||
| currentWinningScore = wordScore; | ||
| } else if (currentWinningScore === wordScore) { | ||
| if (currentWinningWord.length > word.length) { | ||
| if (currentWinningWord.length !== 7) | ||
| currentWinningWord = word; | ||
| } else if (word.length === 7) { | ||
| currentWinningWord = word; | ||
| } | ||
| } | ||
| }); | ||
| return currentWinningWord | ||
| }, | ||
|
|
||
| }; | ||
|
|
||
| Scrabble.Player = class { | ||
| // TODO: implement the Player class | ||
| constructor(name) { | ||
| if (typeof name !== 'string') { | ||
| throw new Error("Name can't be blank"); | ||
| } | ||
| this.name = name; | ||
| this.plays = []; | ||
| } | ||
|
|
||
| hasWon() { | ||
| if (this.totalScore() >= 100) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| play(word) { | ||
| if (typeof word !== 'string' || typeof word === 'number') { | ||
| throw new Error('Not a valid word.'); | ||
| } | ||
| if (this.hasWon() === false) { | ||
| this.plays.push(word); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| totalScore() { | ||
| let sumScore = 0; | ||
| this.plays.forEach((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. Just FYI there is a |
||
| sumScore += Scrabble.score(word); | ||
| }); | ||
| return sumScore; | ||
| } | ||
|
|
||
|
|
||
| highestScoringWord() { | ||
| return Scrabble.highestScoreFrom(this.plays); | ||
| } | ||
|
|
||
| highestWordScore() { | ||
| return Scrabble.score(this.highestScoringWord()); | ||
| } | ||
| }; | ||
|
|
||
| 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 also be written:
score(word) {