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.

125 changes: 118 additions & 7 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,125 @@
const Scrabble = {
score: function(word) {
// TODO: implement score
}
_isValidWord: function _isValidWord(word) {
if ((typeof word === 'string') && (/^[a-zA-Z]+$/.test(word)) && (word.length < 8) && (word.length > 0)) {
return true;
} throw new Error('Invalid Word');
},
score: function score(word) {
const letters = {
1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
2: ['D', 'G'],
3: ['B', 'C', 'M', 'P'],
4: ['F', 'H', 'V', 'W', 'Y'],
5: ['K'],
8: ['J', 'X'],
10: ['Q', 'Z'],
};
const keys = Object.keys(letters);
let totalScore = 0;
if (Scrabble._isValidWord(word)) {
const chars = word.toUpperCase().match(/\S/g);
chars.forEach((element) => {
keys.forEach((num) => {
const numLetters = letters[num];
if (numLetters.includes(element)) {
totalScore += parseFloat(num);
}
});
});
if (word.length === 7) {
totalScore += 50;
}
}
return totalScore;
},

highestScoreFrom: function highestScoreFrom(array) {
if ((!Array.isArray(array)) || (array.length === 0)) {
throw new Error('Invalid Array');
}
const words = array.map(word => new Scrabble.Word(word));
words.sort(Scrabble._compareWordScores);
return words.slice(-1)[0].word;
},

// TODO: add the highestScoreFrom method
_compareWordScores: function _compareWordScores(a, b) {
const scoreA = a.score;
const scoreB = b.score;
const lengthA = a.word.length;
const lengthB = b.word.length;

};
let comparison = 0;
if (scoreA > scoreB) { // greater score wins
comparison = 1;
} else if (scoreA < scoreB) { // lower score loses
comparison = -1;
} else if (lengthA === 7) { // tied score, 7 letter word wins
comparison = 1;
} else if (lengthB === 7) { // tied lose if other word is 7 letters
comparison = -1;
} else if (lengthA < lengthB) { // tied shorter word wins,
comparison = 1;
} else if (lengthA > lengthB) { // tied longer word loses
comparison = -1;
} else if (lengthA === lengthB) { // return first word
comparison = 1;
}
return comparison;
},
}; // end of class!

Scrabble.Player = class {
// TODO: implement the Player class
};
constructor(name) {
if (Scrabble.Player._isValidName(name)) {
this.name = name;
this.plays = [];
this.words = [];
}
}

static _isValidName(name) {
if (name.length > 0) {
return true;
} throw new Error('Invalid Name');
}

play(word) {
if (!this.hasWon()) {
const playedWord = new Scrabble.Word(word);
this.plays.push(playedWord.word);
this.words.push(playedWord);
this.words.sort(Scrabble._compareWordScores);
this.totalScore();
return playedWord.word;
} return false;
}

hasWon() {
const currentScore = this.totalScore();
if (currentScore >= 100) {
return true;
} return false;
}

totalScore() {
const score = this.words.reduce((a, b) => a + b.score, 0);
return score;
}

highestScoringWord() {
return this.words.slice(-1)[0].word;
}

highestWordScore() {
return this.words.slice(-1)[0].score;
}
}; // end of Player class

Scrabble.Word = class {
constructor(word) {
this.word = word;
this.score = Scrabble.score(word);
}
}; // end of Word class

module.exports = Scrabble;
2 changes: 2 additions & 0 deletions spec/scrabble_spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

/* eslint-disable */
const Scrabble = require('../scrabble');

describe('score', function() {
Expand Down