-
Notifications
You must be signed in to change notification settings - Fork 41
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
<pipes><Roxanne Agerone><js scrabble> #37
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,127 @@ | ||
// try { | ||
// if(x == "") throw "empty"; | ||
// if(isNaN(x)) throw "not a number"; | ||
// x = Number(x); | ||
// if(x < 5) throw "too low"; | ||
// if(x > 10) throw "too high"; | ||
// } | ||
// catch(err) { | ||
// message.innerHTML = "Input is " + err; | ||
// } | ||
// do this inside of your stuff | ||
// | ||
// const UserException = function UserException(message) { | ||
// this.message = message; | ||
// this.name = 'UserException'; | ||
// } | ||
// throw new ScrabbleException('InvalidMonthNo'); | ||
|
||
const Scrabble = { | ||
|
||
score: function(word) { | ||
// TODO: implement score | ||
} | ||
let wordArray = word.split(''); | ||
let letter_points = { '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 }; | ||
let score = 0; | ||
if (wordArray.length === 7){ | ||
score += 50; | ||
} else if (wordArray.length > 7){ | ||
throw `This word is too long. Are you sure you aren't cheating?`; | ||
} else if (wordArray.length === 0){ | ||
throw 'You have to choose a word.'; | ||
} else{} | ||
|
||
|
||
|
||
for (let letter of wordArray){ | ||
let point = letter_points[letter.toUpperCase()]; | ||
// try { | ||
if(point === undefined) { | ||
throw 'not found'; | ||
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. Remember that the messages you throw will probably be seen by the user. |
||
} | ||
|
||
// TODO: add the highestScoreFrom method | ||
score += point; | ||
} | ||
|
||
return score; | ||
}, | ||
highestScoreFrom: function(arrayOfWords) { | ||
if (arrayOfWords.length === 0 || arrayOfWords === "") { | ||
throw 'The input is not what I expected.'; | ||
} else if (arrayOfWords.length === 1){ | ||
return arrayOfWords[0]; | ||
} | ||
let highestScore = 0; | ||
let highestScoringWord = ''; | ||
for (let word of arrayOfWords){ | ||
let wordScore = this.score(word); | ||
|
||
|
||
if (wordScore > highestScore || (highestScore === wordScore && word.length < highestScoringWord.length && highestScoringWord.length != 7) || (highestScore === wordScore && word.length === 7)){ | ||
highestScore = wordScore; | ||
highestScoringWord = 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. This if logic is really long! Could you break it up into multiple lines? Something like if (wordScore > highestScore ||
(highestScore === wordScore && word.length < highestScoringWord.length && highestScoringWord.length != 7) ||
(highestScore === wordScore && word.length === 7)) { would be easier to read. |
||
} | ||
} | ||
return highestScoringWord; | ||
} | ||
|
||
}; | ||
|
||
Scrabble.Player = class { | ||
// TODO: implement the Player class | ||
constructor(name) { | ||
this.name = name; | ||
this.plays = []; | ||
if (this.name === null || this.name === '' || this.name === undefined){ | ||
throw 'A Scrabble player needs a name'; | ||
} | ||
} | ||
|
||
totalScore() { | ||
let score = 0; | ||
|
||
if (this.plays.length === 0) { | ||
return score; | ||
} | ||
for(let word of this.plays){ | ||
score += Scrabble.score(word); | ||
} | ||
return score; | ||
} | ||
|
||
hasWon(){ | ||
let winningPoints = 100; | ||
let currentScore = this.totalScore(); | ||
if(currentScore >= winningPoints){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
play(word){ | ||
let matcher = /[A-Za-z]+/; | ||
let matches = word.match(matcher); | ||
if (word === undefined || matches[0].length != word.length){ | ||
throw 'You have to give a REAL word.'; | ||
} | ||
let won = this.hasWon(); | ||
if( won === true){ | ||
return false; | ||
} | ||
this.plays.push(word); | ||
return word; | ||
} | ||
|
||
|
||
highestScoringWord(){ | ||
let playArray = this.plays; | ||
let highestWord = Scrabble.highestScoreFrom(playArray); | ||
return highestWord; | ||
} | ||
|
||
highestWordScore(){ | ||
let highestWord = this.highestScoringWord(); | ||
let highestScore = Scrabble.score(highestWord); | ||
return highestScore; | ||
} | ||
}; | ||
|
||
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.
You don't need this final empty
else{}
clause, you can just close the lastelse if
.