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.

106 changes: 102 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,112 @@
const Scrabble = {

score: function(word) {
// TODO: implement score
}
let scoringHash = { A: 1, E: 1, I: 1, O: 1, U: 1, L: 1, N: 1,
R: 1, S: 1, T: 1, D: 2, G: 2, B: 3, C: 3,
M: 3, P:3, F: 4, H: 4, V: 4, W: 4, Y: 4, K: 5,
J: 8, X: 8, Q: 10, Z: 10
};

let score = 0;

if (word.length < 1) {
throw new Error ('Cannot score - no word entered');
}

if (word.length >= 1) {
if ((/^[a-zA-Z]+$/.test(word)) == false) {
throw('Word must contain only letters A-Z');
}
}

let letters = word.toUpperCase().split('');

// TODO: add the highestScoreFrom method
if (letters.length == 7) {
score += 50
} else if (letters.length > 7) {
throw('Maximum 7 letters are allowed!');
}

letters.forEach(function(letter) {
score += scoringHash[letter];
})

return score;
},

highestScoreFrom: function(arrayOfWords) {
let highestScoreWord = '';
if (arrayOfWords.length < 1) {
throw('No words have been played yet');
} else {
arrayOfWords.forEach(function(word) {
if (highestScoreWord === '' || Scrabble.score(word) > Scrabble.score(highestScoreWord)) {
highestScoreWord = word;
}
else if (Scrabble.score(word) === Scrabble.score(highestScoreWord)) {
if (word.length === 7 && word.length > highestScoreWord.length) {
highestScoreWord = word;
} else if (highestScoreWord.length < 7 && word.length < highestScoreWord.length) {
highestScoreWord = word;
}
}
}
)}

return highestScoreWord;
}
};

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

if (name === undefined) {
throw('Player must have a name');
}
}

play(word) {
if (this.hasWon() === true) {
return false;
} else {
let wordScore = Scrabble.score(word);
this.plays.push(word);
return true;
}
};

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

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

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

totalScore() {
let total = 0;
let allPlays = this.plays

if (allPlays === undefined) {
return total;
} else {
allPlays.forEach(function(word) {
total += Scrabble.score(word);
});
}
return total
}

};

module.exports = Scrabble;