From bdfa9d2b9b3b4deb5121236f18995c949ecb85ce Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Tue, 16 May 2017 14:09:49 -0700 Subject: [PATCH 01/10] Implement score(word) method for Scrabble class --- scrabble.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index a7d0745..791cc82 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,22 @@ -var Scrabble = function() {}; +var Scrabble = function() { + this.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 + } +}; -// YOUR CODE HERE -Scrabble.prototype.helloWorld = function() { - return 'hello world!'; +// Returns the total score value for the given word. +Scrabble.prototype.score = function(word) { + // check that given word only contains letters a-z and <= 7 letters long + var score = 0 + for (i = 0; i < word.length; i++) { + var letter = word[i].toUpperCase(); + score += this.scoreChart[letter]; + } + return ( word.length === 7 ) ? score + 50 : score; }; +var my_score = new Scrabble(); +console.log(my_score.score("aoIOUAA")); module.exports = Scrabble; From 6707c2676b1ad786afcea4c8fe7f838ce1119e77 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 11:55:33 -0700 Subject: [PATCH 02/10] Implement method to find highest score word --- scrabble.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/scrabble.js b/scrabble.js index 791cc82..a02687c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -17,6 +17,50 @@ Scrabble.prototype.score = function(word) { return ( word.length === 7 ) ? score + 50 : score; }; +Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + // check that is an array + // var maxScore = arrayOfWords.find() + + // var topWords = []; + // // var maxScore = 0; + // var self = this; + // var topWords = arrayOfWords.reduce(function(accumulator, 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; + // } + // } + // + var topWord; + 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; +}; +//legnth is 7 or min legnth +// if (letter.match(/[a-z]/i) !== null) { + var my_score = new Scrabble(); console.log(my_score.score("aoIOUAA")); +console.log(my_score.highestScoreFrom(["PURPLE", "PINK"])); module.exports = Scrabble; From e812b477cd61118289a182be3099efcf71eae34e Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 12:18:28 -0700 Subject: [PATCH 03/10] Add Scrabble class that is instantiated with a given name --- player.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 player.js diff --git a/player.js b/player.js new file mode 100644 index 0000000..12757f4 --- /dev/null +++ b/player.js @@ -0,0 +1,6 @@ +var Scrabble = require('./scrabble'); + +var Player = function(name) { + this.name = name; + this.plays = []; +}; From 136c1de25ee2bb099350e88e5a0ad6be2e04f087 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 13:19:11 -0700 Subject: [PATCH 04/10] Implement totalScore method for Player class --- player.js | 17 ++++++++++++++++- scrabble.js | 7 ++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/player.js b/player.js index 12757f4..17c4265 100644 --- a/player.js +++ b/player.js @@ -2,5 +2,20 @@ var Scrabble = require('./scrabble'); var Player = function(name) { this.name = name; - this.plays = []; + this.plays = ["PURPLE", "PINK"]; + this.scrabble = new Scrabble(); }; + +Player.prototype.totalScore = function() { + var totalScore = 0; + var self = this; + this.plays.forEach(function(word) { + totalScore += self.scrabble.score(word); + }) + return totalScore; +}; + + + +var my_player = new Player('Ada'); +console.log(my_player.totalScore()); diff --git a/scrabble.js b/scrabble.js index a02687c..e2a27c7 100644 --- a/scrabble.js +++ b/scrabble.js @@ -60,7 +60,8 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { //legnth is 7 or min legnth // if (letter.match(/[a-z]/i) !== null) { -var my_score = new Scrabble(); -console.log(my_score.score("aoIOUAA")); -console.log(my_score.highestScoreFrom(["PURPLE", "PINK"])); +// var my_score = new Scrabble(); +// console.log(my_score.score("aoIOUAA")); +// console.log(my_score.score("pink")); +// console.log(my_score.highestScoreFrom(["PURPLE", "PINK"])); module.exports = Scrabble; From ab56ba39fb57a16bac4a21e95df8fd8486ce32de Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 13:24:57 -0700 Subject: [PATCH 05/10] Add function to determine if a player has won --- player.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/player.js b/player.js index 17c4265..10cc77c 100644 --- a/player.js +++ b/player.js @@ -2,7 +2,7 @@ var Scrabble = require('./scrabble'); var Player = function(name) { this.name = name; - this.plays = ["PURPLE", "PINK"]; + this.plays = ["PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK"]; this.scrabble = new Scrabble(); }; @@ -15,7 +15,10 @@ Player.prototype.totalScore = function() { return totalScore; }; - +Player.prototype.hasWon = function() { + return ( this.totalScore() >= 100 ) ? true : false; +} var my_player = new Player('Ada'); console.log(my_player.totalScore()); +console.log(my_player.hasWon()); From aa29bcbe108ee1117cae21c3cfffd28cbdae5f74 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 13:28:58 -0700 Subject: [PATCH 06/10] Implement methods to return highest scoring word and highest word score for player class --- player.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/player.js b/player.js index 10cc77c..ad9c24a 100644 --- a/player.js +++ b/player.js @@ -17,8 +17,19 @@ Player.prototype.totalScore = function() { Player.prototype.hasWon = function() { return ( this.totalScore() >= 100 ) ? true : false; -} +}; + +Player.prototype.highestScoringWord = function() { + return this.scrabble.highestScoreFrom(this.plays); +}; + +Player.prototype.highestWordScore = function() { + topWord = this.highestScoringWord(); + return this.scrabble.score(topWord); +}; 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()); From 883856950d9b9f2bb456f166683463a884fc9a71 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 15:00:00 -0700 Subject: [PATCH 07/10] Add method to allow a Player to play a new word --- player.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/player.js b/player.js index ad9c24a..36038c4 100644 --- a/player.js +++ b/player.js @@ -2,10 +2,17 @@ var Scrabble = require('./scrabble'); var Player = function(name) { this.name = name; - this.plays = ["PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK", "PURPLE", "PINK"]; + this.plays = ["PURPLE", "PINK"]; this.scrabble = new Scrabble(); }; +Player.prototype.play = function(word) { + if (this.hasWon()) { return false }; + // check to see if valid word? + this.plays.push(word); + // return score? +}; + Player.prototype.totalScore = function() { var totalScore = 0; var self = this; @@ -28,8 +35,11 @@ Player.prototype.highestWordScore = function() { return this.scrabble.score(topWord); }; -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()); +// 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.highestScoringWord()); From 91948a513bfbf0f7e57bc42293fe594465c33374 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 15:19:48 -0700 Subject: [PATCH 08/10] Limit scoring to words that only contain chars a-z and are <= 7 letters long --- scrabble.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/scrabble.js b/scrabble.js index e2a27c7..479f2c1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,18 +1,20 @@ -var Scrabble = function() { - this.scoreChart = { +var Scrabble = function() {}; + +// 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 ((word.length > 7) || (!/^[a-zA-Z]+$/.test(word))) { return null }; + + 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 - } -}; + }; -// Returns the total score value for the given word. -Scrabble.prototype.score = function(word) { - // check that given word only contains letters a-z and <= 7 letters long var score = 0 for (i = 0; i < word.length; i++) { var letter = word[i].toUpperCase(); - score += this.scoreChart[letter]; + score += scoreChart[letter]; } return ( word.length === 7 ) ? score + 50 : score; }; @@ -60,8 +62,10 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { //legnth is 7 or min legnth // if (letter.match(/[a-z]/i) !== null) { -// var my_score = new Scrabble(); -// console.log(my_score.score("aoIOUAA")); -// console.log(my_score.score("pink")); +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("hel9lo")); // console.log(my_score.highestScoreFrom(["PURPLE", "PINK"])); module.exports = Scrabble; From d5f8cbd30a4b913bb6874f32ae266d69265bc804 Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 21:41:59 -0700 Subject: [PATCH 09/10] Refractor highestScoreFrom method, adding a highestScore method to split method functionality --- player.js | 2 + scrabble.js | 111 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/player.js b/player.js index 36038c4..c9a1087 100644 --- a/player.js +++ b/player.js @@ -27,10 +27,12 @@ Player.prototype.hasWon = function() { }; Player.prototype.highestScoringWord = function() { + // if plays empty? return this.scrabble.highestScoreFrom(this.plays); }; Player.prototype.highestWordScore = function() { + // if plays empty? topWord = this.highestScoringWord(); return this.scrabble.score(topWord); }; diff --git a/scrabble.js b/scrabble.js index 479f2c1..3d0d77a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -3,7 +3,9 @@ var Scrabble = function() {}; // 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 ((word.length > 7) || (!/^[a-zA-Z]+$/.test(word))) { return null }; + if ((!(word instanceof String)) || !/^[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, @@ -19,53 +21,72 @@ Scrabble.prototype.score = function(word) { return ( word.length === 7 ) ? score + 50 : score; }; -Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { - // check that is an array - // var maxScore = arrayOfWords.find() +// 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] }; - // var topWords = []; - // // var maxScore = 0; - // var self = this; - // var topWords = arrayOfWords.reduce(function(accumulator, 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; - // } - // } - // - var topWord; - 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; +// 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; }; -//legnth is 7 or min legnth -// if (letter.match(/[a-z]/i) !== null) { + +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; +}; + 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("hel9lo")); +// console.log(my_score.score(undefined)); +// 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")); module.exports = Scrabble; From dc7b30f2fb3e3d137189ad6323ded767a62c93de Mon Sep 17 00:00:00 2001 From: Cara Comfort Date: Wed, 17 May 2017 22:00:41 -0700 Subject: [PATCH 10/10] Update player highestWordScore with new scrabble highestScore function --- player.js | 22 +++++++++++++--------- scrabble.js | 9 +++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/player.js b/player.js index c9a1087..edb56ab 100644 --- a/player.js +++ b/player.js @@ -1,16 +1,18 @@ var Scrabble = require('./scrabble'); -var Player = function(name) { +var Player = function(name, game= new Scrabble()) { this.name = name; this.plays = ["PURPLE", "PINK"]; - this.scrabble = new Scrabble(); + this.scrabble = game; }; Player.prototype.play = function(word) { if (this.hasWon()) { return false }; - // check to see if valid word? - this.plays.push(word); - // return score? + + 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() { @@ -27,14 +29,11 @@ Player.prototype.hasWon = function() { }; Player.prototype.highestScoringWord = function() { - // if plays empty? return this.scrabble.highestScoreFrom(this.plays); }; Player.prototype.highestWordScore = function() { - // if plays empty? - topWord = this.highestScoringWord(); - return this.scrabble.score(topWord); + return this.scrabble.highestScore(this.plays); }; // var my_player = new Player('Ada'); @@ -44,4 +43,9 @@ Player.prototype.highestWordScore = function() { // 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 3d0d77a..f581172 100644 --- a/scrabble.js +++ b/scrabble.js @@ -3,9 +3,7 @@ var Scrabble = function() {}; // 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 ((!(word instanceof String)) || !/^[a-zA-Z]+$/.test(word) || word.length > 7) { - return 0 - }; + 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, @@ -77,9 +75,9 @@ Scrabble.prototype.highestScore = function(words) { // return topScore; }; +module.exports = Scrabble; -var my_score = new Scrabble(); -// console.log(my_score.score(undefined)); +// var my_score = new Scrabble(); // console.log(my_score.score("aoIOUAA")); // console.log(my_score.score("pink")); // console.log(my_score.score("elephant")); @@ -89,4 +87,3 @@ var my_score = new Scrabble(); // console.log(my_score.highestScoreFrom(["rainbov", "rainbow"])); // console.log(my_score.highestScoreFrom([])); // console.log(my_score.highestScoreFrom("elephan")); -module.exports = Scrabble;