diff --git a/player.js b/player.js new file mode 100644 index 0000000..edb56ab --- /dev/null +++ b/player.js @@ -0,0 +1,51 @@ +var Scrabble = require('./scrabble'); + +var Player = function(name, game= new Scrabble()) { + this.name = name; + this.plays = ["PURPLE", "PINK"]; + this.scrabble = game; +}; + +Player.prototype.play = function(word) { + if (this.hasWon()) { return false }; + + var score = this.scrabble.score(word); + // Only add word to plays array if it is a valid word. + if (score !== 0) { this.plays.push(word) }; + return score; +}; + +Player.prototype.totalScore = function() { + var totalScore = 0; + var self = this; + this.plays.forEach(function(word) { + totalScore += self.scrabble.score(word); + }) + return totalScore; +}; + +Player.prototype.hasWon = function() { + return ( this.totalScore() >= 100 ) ? true : false; +}; + +Player.prototype.highestScoringWord = function() { + return this.scrabble.highestScoreFrom(this.plays); +}; + +Player.prototype.highestWordScore = function() { + return this.scrabble.highestScore(this.plays); +}; + +// var my_player = new Player('Ada'); +// console.log(my_player.totalScore()); +// console.log(my_player.hasWon()); +// console.log(my_player.highestScoringWord()); +// console.log(my_player.highestWordScore()); +// console.log(my_player.play("happy")); +// console.log(my_player.plays); +// console.log(my_player.play("")); +// console.log(my_player.plays); +// console.log(my_player.play(8)); +// console.log(my_player.plays); +// console.log(my_player.highestScoringWord()); +// console.log(my_player.highestWordScore()); diff --git a/scrabble.js b/scrabble.js index a7d0745..f581172 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,89 @@ var Scrabble = function() {}; -// YOUR CODE HERE -Scrabble.prototype.helloWorld = function() { - return 'hello world!'; +// Returns the total score value for the given word. +Scrabble.prototype.score = function(word) { + // Checks if word is longer than 7 letters or contains a char other than a-z. + if (!/^[a-zA-Z]+$/.test(word) || word.length > 7) { return 0 }; + + var scoreChart = { + '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 + }; + + var score = 0 + for (i = 0; i < word.length; i++) { + var letter = word[i].toUpperCase(); + score += scoreChart[letter]; + } + return ( word.length === 7 ) ? score + 50 : score; +}; + +// Returns the word in the given array with the highest score. +Scrabble.prototype.highestScoreFrom = function(words) { +var maxScore = this.highestScore(words); +// Checks if the array given is valid. +if (maxScore === 0) { return null }; + +// Finds the highest scoring word(s). +var topWords = words.filter(function(word) { + return self.score(word) === maxScore; +}); + +// If there is only one highest scoring word, return that. +if (topWords.length === 1) { return topWords[0] }; + +// If there's a tie, checks if there is a highest score word with a length of 7. +var topWord = topWords.find(function(word) { return word.length === 7 }); +// Otherwise, looks for the shortest word. +return topWord || topWords.reduce((prev, curr) => (prev.length <= curr.length) ? prev : curr); + +// Alternative solution +// var topWord = null; +// var maxScore = 0; +// var self = this; +// arrayOfWords.forEach(function(word) { +// score = self.score(word); +// if (score > maxScore) { +// topWord = word; +// maxScore = score; +// } else if (score === maxScore && word.length != topWord.length) { +// if (word.length === 7 || word.length < topWord.length) { +// topWord = word; +// maxScore = score; +// } +// } +// }); +// return topWord; +}; + +Scrabble.prototype.highestScore = function(words) { + // Checks if input is an array. Otherwise, returns 0. + if (!(words instanceof Array) || (words.length === 0)) { return 0 }; + + self = this; + var scores = words.map(function(word) { + return self.score(word); + }); + return Math.max(...scores); + + // Alternative solution + // var topScore = arrayOfWords.reduce(function(topScore, currWord) { + // var currScore = self.score(currWord); + // return Math.max(topScore, currScore); + // }, 0); + // return topScore; }; module.exports = Scrabble; + +// var my_score = new Scrabble(); +// console.log(my_score.score("aoIOUAA")); +// console.log(my_score.score("pink")); +// console.log(my_score.score("elephant")); +// console.log(my_score.score("")); +// console.log(my_score.highestScoreFrom(["PURPLE", "PINK", "rainbow"])); +// console.log(my_score.highestScoreFrom(["PURPLE", "PINK"])); +// console.log(my_score.highestScoreFrom(["rainbov", "rainbow"])); +// console.log(my_score.highestScoreFrom([])); +// console.log(my_score.highestScoreFrom("elephan"));