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

<pipes><Roxanne Agerone><js scrabble> #37

Open
wants to merge 3 commits into
base: master
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
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 118 additions & 5 deletions scrabble.js
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{}

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 last else if.




for (let letter of wordArray){
let point = letter_points[letter.toUpperCase()];
// try {
if(point === undefined) {
throw 'not found';

Choose a reason for hiding this comment

The 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. 'not found' might not give them enough context to figure out what went wrong - something like `invalid letter ${ letter }`.

}

// 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;

Choose a reason for hiding this comment

The 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;
1 change: 1 addition & 0 deletions spec/scrabble_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ describe('Player', function() {

expect(player.play('dog')).toBe(false);
expect(player.plays.length).toBe(1);

});
});

Expand Down