Skip to content
Open
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
133 changes: 124 additions & 9 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,129 @@
const scrabbleLetters = {
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,
};

const Scrabble = {
score: function(word) {
// TODO: implement score
}
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 be written as:

score(word) {


let regex = /^[a-zA-Z]+$/;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Making variables you never reassign const is a good idea.


if (!regex.test(word)) {
throw new Error('Invalid characters');
}

if (word.length > 7) {
throw new Error('Word must be 1 - 7 characters long');
}

let lowercaseWord = word.toLowerCase();
let score = 0;


for (let i = 0; i < lowercaseWord.length; i++) {

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 works fine, you can also use reduce to calculate it.

score += scrabbleLetters[lowercaseWord[i]];
}

if (lowercaseWord.length === 7) {
score += 50;
}

return score;
},

highestScoreFrom: function(wordsArray) {

if (!Array.isArray(wordsArray) || wordsArray.length === 0 ) {
throw new Error('Invalid');
}

if (wordsArray.length === 1) {
return wordsArray[0];
}

let max = 0;
let highestWord = '';

for (let i = 0; i < wordsArray.length; i++) {
let score = this.score(wordsArray[i]);

if (score > max) {
max = score;
highestWord = wordsArray[i];
}
else if (score === max) {
if (highestWord.length !== 7 &&
(wordsArray[i].length === 7 || (wordsArray[i].length < highestWord.length))) {
highestWord = wordsArray[i];
}
}
}

return highestWord;
},


};

Scrabble.Player = class {
constructor(name) {
if (name === undefined) {
throw new Error('Invalid - must be a name');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good work creating an object to throw the error.

}
this.name = name;
this.playArray = [];
this.plays = this.playArray;
}

play(word) {
let regex = /^[a-zA-Z]+$/;

if (word === undefined || !regex.test(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.

Nice use of Regex

throw new Error('Invalid word');
}

if (! this.hasWon()) {
if (this.playArray.push(word)) {
return true;
}
} else {
return false;
}
}

totalScore() {
let total = 0;

for (let i = 0; i < this.plays.length; i++) {
total += Scrabble.score(this.plays[i]);
}

return total;
}

hasWon() {

if (this.totalScore() >= 100) {
return true;
} else {
return false;
}
}

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

// TODO: add the highestScoreFrom method
highestWordScore() {
let highestWord = this.highestScoringWord();
return Scrabble.score(highestWord);
}
};

};

Scrabble.Player = class {
// TODO: implement the Player class
};

module.exports = Scrabble;
module.exports = Scrabble;