Skip to content
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.

143 changes: 139 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,149 @@

const Scrabble = {
score: function(word) {

Copy link
Copy Markdown

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) {

// 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("");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you never reassign wordArr making it a const is a good idea.



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) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI there is a .reduce function in JavaScript.

sumScore += Scrabble.score(word);
});
return sumScore;
}


highestScoringWord() {
return Scrabble.highestScoreFrom(this.plays);
}

highestWordScore() {
return Scrabble.score(this.highestScoringWord());
}
};

module.exports = Scrabble;