From 5d671401b5f42b089d2287dc4caeefb8ad8ee238 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 10:47:05 -0800 Subject: [PATCH 01/19] new module file with constants --- scrabble_constants.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 scrabble_constants.js diff --git a/scrabble_constants.js b/scrabble_constants.js new file mode 100644 index 0000000..6f74292 --- /dev/null +++ b/scrabble_constants.js @@ -0,0 +1,37 @@ + +var ScrabbleConstants = { + SCORE_HASH: {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}, + LETTERS: [["A", "A", "A", "A", "A", "A", "A", "A", "A"], + ["B", "B"], + ["C", "C"], + ["D", "D", "D", "D"], + ["E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E"], + ["F", "F"], + ["G", "G", "G"], + ["H", "H"], + ["I", "I", "I", "I", "I", "I", "I", "I", "I"], + ["J"], + ["K"], + ["L", "L", "L", "L"], + ["M", "M"], + ["N", "N", "N", "N", "N", "N"], + ["O", "O", "O", "O", "O", "O", "O", "O"], + ["P", "P"], + ["Q"], + ["R", "R", "R", "R", "R", "R"], + ["S", "S", "S", "S"], + ["T", "T", "T", "T", "T", "T"], + ["U", "U", "U", "U"], + ["V", "V"], + ["W", "W"], + ["X"], + ["Y", "Y"], + ["Z"]], + MAX_TILES: 7 +} + +console.log(ScrabbleConstants.SCORE_HASH) + +module.exports = ScrabbleConstants From 3c8dbb9e73e2d89f8bed1a3e3db3566086d6bb12 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 14:39:00 -0800 Subject: [PATCH 02/19] finished score function --- scrabble.js | 28 +++++++++++++++++++++++++++- scrabble_constants.js | 8 +++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index a7d0745..816fa61 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,34 @@ +var CONSTANTS = require('./scrabble_constants'); + var Scrabble = function() {}; -// YOUR CODE HERE +// this is a thing to copy from Scrabble.prototype.helloWorld = function() { return 'hello world!'; }; +// add score method to Scrabble +Scrabble.prototype.score = function(word){ + word = word.toUpperCase(); + total = 0; + for (let char of word) { + total += CONSTANTS.SCORES[char] + }; + if (word.length >= 7) { + total += 50 + }; + return total; +} + + +// testing for score functions +var scrabble = new Scrabble() + +// regular word +console.log("Score for 'word' is: " + scrabble.score("word")) +// bingo word (7+ letters) +console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) + + +// export this file as a module module.exports = Scrabble; diff --git a/scrabble_constants.js b/scrabble_constants.js index 6f74292..b91701b 100644 --- a/scrabble_constants.js +++ b/scrabble_constants.js @@ -1,6 +1,6 @@ -var ScrabbleConstants = { - SCORE_HASH: {A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J:8, +var SCRABBLE_CONSTANTS = { + SCORES: {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}, LETTERS: [["A", "A", "A", "A", "A", "A", "A", "A", "A"], @@ -32,6 +32,4 @@ var ScrabbleConstants = { MAX_TILES: 7 } -console.log(ScrabbleConstants.SCORE_HASH) - -module.exports = ScrabbleConstants +module.exports = SCRABBLE_CONSTANTS From 355305d02752aed6206b01dc7a5acb4ad8682590 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:03:24 -0800 Subject: [PATCH 03/19] added highestScoreFrom method, though it is still pretty minimal --- scrabble.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 816fa61..2f02ca2 100644 --- a/scrabble.js +++ b/scrabble.js @@ -10,7 +10,7 @@ Scrabble.prototype.helloWorld = function() { // add score method to Scrabble Scrabble.prototype.score = function(word){ word = word.toUpperCase(); - total = 0; + var total = 0; for (let char of word) { total += CONSTANTS.SCORES[char] }; @@ -20,15 +20,38 @@ Scrabble.prototype.score = function(word){ return total; } +Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + var highestWord = null; + var highestWordScore = null; + var scoreArray = []; -// testing for score functions +// iterate through words, score them all + for (let word of arrayOfWords) { + scoreArray.push(Scrabble.prototype.score(word)); + }; + +// iterate through scores, find best based on conditional + for (var i = 0; i < scoreArray.length; i++) { + if (scoreArray[i] > highestWordScore) { + highestWordScore = scoreArray[i] + highestWord = arrayOfWords[i]; + } + }; + return highestWord +}; + +// new scrabble var scrabble = new Scrabble() -// regular word +// testing for score function// regular word console.log("Score for 'word' is: " + scrabble.score("word")) // bingo word (7+ letters) console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) +console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) +// testing highestScoreFrom fx +console.log("highest score word is: " + scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) + // export this file as a module module.exports = Scrabble; From af35fa02f1d8e007c3f0b611f869d2a76199dcb4 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:35:23 -0800 Subject: [PATCH 04/19] highestScoreFrom method now meets all specifications --- scrabble.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index 2f02ca2..1cd1552 100644 --- a/scrabble.js +++ b/scrabble.js @@ -33,9 +33,15 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { // iterate through scores, find best based on conditional for (var i = 0; i < scoreArray.length; i++) { if (scoreArray[i] > highestWordScore) { - highestWordScore = scoreArray[i] + highestWordScore = scoreArray[i]; highestWord = arrayOfWords[i]; - } + } else if (scoreArray[i] == highestWordScore) { + if (highestWord.length < 7 && arrayOfWords[i].length >= 7) { + highestWordScore = scoreArray[i] + highestWord = arrayOfWords[i]; + }; + }; + }; return highestWord }; @@ -43,15 +49,23 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { // new scrabble var scrabble = new Scrabble() -// testing for score function// regular word +// testing for score fx// regular word console.log("Score for 'word' is: " + scrabble.score("word")) // bingo word (7+ letters) console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) -console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) // testing highestScoreFrom fx +console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) console.log("highest score word is: " + scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) +// test that in the case of an equal score, the word that is 7 letters or greater should be the winner +console.log("zzzzzz: " + scrabble.score("zzzzzz") + ", iiiiiif: " + scrabble.score("iiiiiif")) +console.log("highest score word is: " + scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) + +// test that in the case of an equal score where none is 7 letters, the first should be the winner +console.log("aaaaaa: " + scrabble.score("aaaaaa") + ", iiiiii: " + scrabble.score("iiiiii")) +console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) + // export this file as a module module.exports = Scrabble; From fc4e03b4b8c11868cb3edda5c4260c5d8df3fc2e Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:35:47 -0800 Subject: [PATCH 05/19] added a comment to explain myself better --- scrabble.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1cd1552..2590841 100644 --- a/scrabble.js +++ b/scrabble.js @@ -35,7 +35,9 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { if (scoreArray[i] > highestWordScore) { highestWordScore = scoreArray[i]; highestWord = arrayOfWords[i]; - } else if (scoreArray[i] == highestWordScore) { + } + // if the scores are tied, check to see if one of the words is longer than 7 letters. if neither is, continue on as if nothing happened (this will effectively choose the first tied score to be the winner) // + else if (scoreArray[i] == highestWordScore) { if (highestWord.length < 7 && arrayOfWords[i].length >= 7) { highestWordScore = scoreArray[i] highestWord = arrayOfWords[i]; @@ -64,7 +66,7 @@ console.log("highest score word is: " + scrabble.highestScoreFrom(["zzzzzz", "ii // test that in the case of an equal score where none is 7 letters, the first should be the winner console.log("aaaaaa: " + scrabble.score("aaaaaa") + ", iiiiii: " + scrabble.score("iiiiii")) -console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) +console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) // export this file as a module From 58fb010286a2fa80f34ad28b18ffb0f524758fca Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:44:11 -0800 Subject: [PATCH 06/19] new file for player module yay --- player.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 player.js diff --git a/player.js b/player.js new file mode 100644 index 0000000..e0da456 --- /dev/null +++ b/player.js @@ -0,0 +1,9 @@ +var CONSTANTS = require('./scrabble_constants'); +var Scrabble = require('./scrabble') + +// constructor! +var Player = function() {}; + + +// export this file as a module +module.exports = Player; From 1f5260974420e7715753ef5abd5a5cdaaf841f5a Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:48:00 -0800 Subject: [PATCH 07/19] instantiating players with name and plays --- player.js | 9 ++++++++- scrabble.js | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/player.js b/player.js index e0da456..3a4aacb 100644 --- a/player.js +++ b/player.js @@ -2,8 +2,15 @@ var CONSTANTS = require('./scrabble_constants'); var Scrabble = require('./scrabble') // constructor! -var Player = function() {}; +var Player = function(name) { + this._name = name; + this._plays = []; +}; +// testing that we can instantiate a new Player +var player = new Player("Edith"); +console.log("Player is: " + player._name); +console.log("Player's plays:" + player._plays); // export this file as a module module.exports = Player; diff --git a/scrabble.js b/scrabble.js index 2590841..a5c520e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,5 +1,6 @@ var CONSTANTS = require('./scrabble_constants'); +// constructor! var Scrabble = function() {}; // this is a thing to copy from From 6c6386fb97f24f1ca0a757be33c8cf0f44bd3b85 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 15:48:17 -0800 Subject: [PATCH 08/19] comment out manual testing in scrabble.js --- scrabble.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/scrabble.js b/scrabble.js index a5c520e..ce6975f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -37,7 +37,7 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { highestWordScore = scoreArray[i]; highestWord = arrayOfWords[i]; } - // if the scores are tied, check to see if one of the words is longer than 7 letters. if neither is, continue on as if nothing happened (this will effectively choose the first tied score to be the winner) // + // if the scores are tied, check to see if one of the words is longer than 7 letters. if neither is, continue on as if nothing happened (this will effectively choose the first tied score to be the winner) // else if (scoreArray[i] == highestWordScore) { if (highestWord.length < 7 && arrayOfWords[i].length >= 7) { highestWordScore = scoreArray[i] @@ -52,23 +52,23 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { // new scrabble var scrabble = new Scrabble() -// testing for score fx// regular word -console.log("Score for 'word' is: " + scrabble.score("word")) -// bingo word (7+ letters) -console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) - -// testing highestScoreFrom fx -console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) -console.log("highest score word is: " + scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) - -// test that in the case of an equal score, the word that is 7 letters or greater should be the winner -console.log("zzzzzz: " + scrabble.score("zzzzzz") + ", iiiiiif: " + scrabble.score("iiiiiif")) -console.log("highest score word is: " + scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) - -// test that in the case of an equal score where none is 7 letters, the first should be the winner -console.log("aaaaaa: " + scrabble.score("aaaaaa") + ", iiiiii: " + scrabble.score("iiiiii")) -console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) - +// // testing for score fx// regular word +// console.log("Score for 'word' is: " + scrabble.score("word")) +// // bingo word (7+ letters) +// console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) +// +// // testing highestScoreFrom fx +// console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) +// console.log("highest score word is: " + scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) +// +// // test that in the case of an equal score, the word that is 7 letters or greater should be the winner +// console.log("zzzzzz: " + scrabble.score("zzzzzz") + ", iiiiiif: " + scrabble.score("iiiiiif")) +// console.log("highest score word is: " + scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) +// +// // test that in the case of an equal score where none is 7 letters, the first should be the winner +// console.log("aaaaaa: " + scrabble.score("aaaaaa") + ", iiiiii: " + scrabble.score("iiiiii")) +// console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) +// // export this file as a module module.exports = Scrabble; From 135529e6af43a95fae796cab4cd21f10ce69aa4c Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 19:36:00 -0800 Subject: [PATCH 09/19] change protype functions to non-prototype functions after realizing that that will work better with player module --- scrabble.js | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/scrabble.js b/scrabble.js index ce6975f..0da483e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -4,12 +4,12 @@ var CONSTANTS = require('./scrabble_constants'); var Scrabble = function() {}; // this is a thing to copy from -Scrabble.prototype.helloWorld = function() { +Scrabble.helloWorld = function() { return 'hello world!'; }; // add score method to Scrabble -Scrabble.prototype.score = function(word){ +Scrabble.score = function(word){ word = word.toUpperCase(); var total = 0; for (let char of word) { @@ -21,14 +21,14 @@ Scrabble.prototype.score = function(word){ return total; } -Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { +Scrabble.highestScoreFrom = function(arrayOfWords) { var highestWord = null; var highestWordScore = null; var scoreArray = []; // iterate through words, score them all for (let word of arrayOfWords) { - scoreArray.push(Scrabble.prototype.score(word)); + scoreArray.push(Scrabble.score(word)); }; // iterate through scores, find best based on conditional @@ -49,26 +49,24 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { return highestWord }; -// new scrabble -var scrabble = new Scrabble() - +// console.log(Scrabble.helloWorld()) // // testing for score fx// regular word -// console.log("Score for 'word' is: " + scrabble.score("word")) +// console.log("Score for 'word' is: " + Scrabble.score("word")) // // bingo word (7+ letters) -// console.log("Score for 'foooooooblah' is: " + scrabble.score("foooooooblah")) +// console.log("Score for 'foooooooblah' is: " + Scrabble.score("foooooooblah")) // // // testing highestScoreFrom fx -// console.log("hiii: " + scrabble.score("hiii") + ", bloop: " + scrabble.score("bloop") + ", cat: " + scrabble.score("cat") ) -// console.log("highest score word is: " + scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) +// console.log("hiii: " + Scrabble.score("hiii") + ", bloop: " + Scrabble.score("bloop") + ", cat: " + Scrabble.score("cat") ) +// console.log("highest score word is: " + Scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) // // // test that in the case of an equal score, the word that is 7 letters or greater should be the winner -// console.log("zzzzzz: " + scrabble.score("zzzzzz") + ", iiiiiif: " + scrabble.score("iiiiiif")) -// console.log("highest score word is: " + scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) +// console.log("zzzzzz: " + Scrabble.score("zzzzzz") + ", iiiiiif: " + Scrabble.score("iiiiiif")) +// console.log("highest score word is: " + Scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) // // // test that in the case of an equal score where none is 7 letters, the first should be the winner -// console.log("aaaaaa: " + scrabble.score("aaaaaa") + ", iiiiii: " + scrabble.score("iiiiii")) -// console.log("highest score word is: " + scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) +// console.log("aaaaaa: " + Scrabble.score("aaaaaa") + ", iiiiii: " + Scrabble.score("iiiiii")) +// console.log("highest score word is: " + Scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) +// // // - // export this file as a module module.exports = Scrabble; From b447cbb7eaa01d753cdc530c3cb41e69d4e5e024 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 19:45:18 -0800 Subject: [PATCH 10/19] play method for player --- player.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/player.js b/player.js index 3a4aacb..de1fefb 100644 --- a/player.js +++ b/player.js @@ -5,12 +5,33 @@ var Scrabble = require('./scrabble') var Player = function(name) { this._name = name; this._plays = []; + this._score = 0 + this._won = false; }; +Player.prototype = { + play: function(word){ + if (this._won) { + return false; + } else { + this._plays.push(word); + return this._plays; + }; + } //end of play fx + +} + // testing that we can instantiate a new Player var player = new Player("Edith"); console.log("Player is: " + player._name); -console.log("Player's plays:" + player._plays); - +console.log(player._name + "'s plays: " + player._plays); +console.log(player._name + "'s score: " + player._score); +console.log("Player won? " + player._won) +player.play("cat") +console.log(player._name + "'s plays:" + player._plays); +// player._won = true; +// console.log(player._won); +player.play("bloooop"); +console.log(player._name + "'s plays:" + player._plays); // export this file as a module module.exports = Player; From ea8acb6f8967f33997de9208c0209f0d7ccca9e3 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 20:01:40 -0800 Subject: [PATCH 11/19] totalScore fx --- player.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/player.js b/player.js index de1fefb..e005f21 100644 --- a/player.js +++ b/player.js @@ -17,21 +17,34 @@ Player.prototype = { this._plays.push(word); return this._plays; }; - } //end of play fx - + }, //end of play fx + totalScore(){ + var score = this._score + this._plays.forEach(function(play){ + score += Scrabble.score(play); + }); + this._score = score; + return this._score; + } //end of totalScore fx } // testing that we can instantiate a new Player var player = new Player("Edith"); -console.log("Player is: " + player._name); -console.log(player._name + "'s plays: " + player._plays); -console.log(player._name + "'s score: " + player._score); -console.log("Player won? " + player._won) +// test attributes +// console.log("Player is: " + player._name); +// console.log(player._name + "'s plays: " + player._plays); +// console.log(player._name + "'s score: " + player._score); +// console.log("Player won? " + player._won) +// test whether we can play stuff player.play("cat") console.log(player._name + "'s plays:" + player._plays); +// test ability to set whether player has won // player._won = true; // console.log(player._won); player.play("bloooop"); console.log(player._name + "'s plays:" + player._plays); +// test totalScore fx +console.log("Total score: " + player.totalScore()) + // export this file as a module module.exports = Player; From c90590f5b38020a5cd40e72b99160fbd4429a262 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 20:18:38 -0800 Subject: [PATCH 12/19] totalScore fx --- player.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/player.js b/player.js index e005f21..2cd8cf7 100644 --- a/player.js +++ b/player.js @@ -5,27 +5,33 @@ var Scrabble = require('./scrabble') var Player = function(name) { this._name = name; this._plays = []; - this._score = 0 - this._won = false; }; Player.prototype = { play: function(word){ - if (this._won) { + if (this.hasWon()) { return false; } else { this._plays.push(word); - return this._plays; }; + return this._plays // currently returning the list of all plays }, //end of play fx + totalScore(){ - var score = this._score + var total = 0 this._plays.forEach(function(play){ - score += Scrabble.score(play); + total += Scrabble.score(play); }); - this._score = score; - return this._score; - } //end of totalScore fx + return total; + }, //end of totalScore fx + + hasWon(){ + if (this.totalScore() >= 100) { + return true; + } else { + return false; + } + } //end of hasWon fx } // testing that we can instantiate a new Player @@ -38,13 +44,17 @@ var player = new Player("Edith"); // test whether we can play stuff player.play("cat") console.log(player._name + "'s plays:" + player._plays); -// test ability to set whether player has won -// player._won = true; -// console.log(player._won); -player.play("bloooop"); +player.play("bloop"); console.log(player._name + "'s plays:" + player._plays); // test totalScore fx console.log("Total score: " + player.totalScore()) - +// test hasWon fx +console.log(player.hasWon() + ", player has not won.") +player.play("xxxx") +console.log("Total score: " + player.totalScore()) +player.play("xxxsssx") +console.log(player._name + "'s plays:" + player._plays); +console.log("Total score: " + player.totalScore()) +console.log(player.hasWon() + ", player has won!") // export this file as a module module.exports = Player; From 06ecf50bcd7d691b54593e354f1fb3dfbff34ab6 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Wed, 16 Nov 2016 20:30:12 -0800 Subject: [PATCH 13/19] highestWordScore fx & commenting out manual test stuff --- player.js | 62 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/player.js b/player.js index 2cd8cf7..7c02f4e 100644 --- a/player.js +++ b/player.js @@ -17,7 +17,7 @@ Player.prototype = { return this._plays // currently returning the list of all plays }, //end of play fx - totalScore(){ + totalScore: function(){ var total = 0 this._plays.forEach(function(play){ total += Scrabble.score(play); @@ -25,36 +25,50 @@ Player.prototype = { return total; }, //end of totalScore fx - hasWon(){ + hasWon: function(){ if (this.totalScore() >= 100) { return true; } else { return false; } - } //end of hasWon fx + }, //end of hasWon fx + + highestScoringWord: function(){ + return Scrabble.highestScoreFrom(this._plays); + }, //end of highestscoringword fx + + highestWordScore: function(){ + return Scrabble.score(this.highestScoringWord()); + } //end of highestWordScore } // testing that we can instantiate a new Player -var player = new Player("Edith"); -// test attributes -// console.log("Player is: " + player._name); -// console.log(player._name + "'s plays: " + player._plays); -// console.log(player._name + "'s score: " + player._score); -// console.log("Player won? " + player._won) -// test whether we can play stuff -player.play("cat") -console.log(player._name + "'s plays:" + player._plays); -player.play("bloop"); -console.log(player._name + "'s plays:" + player._plays); -// test totalScore fx -console.log("Total score: " + player.totalScore()) -// test hasWon fx -console.log(player.hasWon() + ", player has not won.") -player.play("xxxx") -console.log("Total score: " + player.totalScore()) -player.play("xxxsssx") -console.log(player._name + "'s plays:" + player._plays); -console.log("Total score: " + player.totalScore()) -console.log(player.hasWon() + ", player has won!") +// var player = new Player("Edith"); +// // test attributes +// // console.log("Player is: " + player._name); +// // console.log(player._name + "'s plays: " + player._plays); +// // console.log(player._name + "'s score: " + player._score); +// // console.log("Player won? " + player._won) +// // test whether we can play stuff +// player.play("cat") +// console.log(player._name + "'s plays:" + player._plays); +// player.play("bloop"); +// console.log(player._name + "'s plays:" + player._plays); +// // test totalScore fx +// console.log("Total score: " + player.totalScore()) +// // test hasWon fx +// console.log(player.hasWon() + ", player has not won.") +// player.play("xxxx") +// console.log("Total score: " + player.totalScore()) +// player.play("xxxsssx") +// console.log(player._name + "'s plays:" + player._plays); +// console.log("Total score: " + player.totalScore()) +// console.log(player.hasWon() + ", player has won!") +// // test highest scoring word fx +// console.log("highest score word is " + player.highestScoringWord()) +// // test highest word score fx +// console.log("score of that word is " + player.highestWordScore()) + + // export this file as a module module.exports = Player; From 6c0376d0d96d22a84391bbe947d00be1be096448 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 10:05:09 -0800 Subject: [PATCH 14/19] implement jasmine --- spec/scrabble_spec.js | 0 spec/support/jasmine.json | 11 +++++++++++ 2 files changed, 11 insertions(+) create mode 100644 spec/scrabble_spec.js create mode 100644 spec/support/jasmine.json diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js new file mode 100644 index 0000000..e69de29 diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json new file mode 100644 index 0000000..3ea3166 --- /dev/null +++ b/spec/support/jasmine.json @@ -0,0 +1,11 @@ +{ + "spec_dir": "spec", + "spec_files": [ + "**/*[sS]pec.js" + ], + "helpers": [ + "helpers/**/*.js" + ], + "stopSpecOnExpectationFailure": false, + "random": false +} From f7fa049bf47a757c6d2fba493f8744cbaea41f55 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 14:24:34 -0800 Subject: [PATCH 15/19] refactor if/else to ensure correct results if words are different length but same score --- scrabble.js | 24 ++++-------------------- scrabble_constants.js | 8 +++++--- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0da483e..4a3a7e7 100644 --- a/scrabble.js +++ b/scrabble.js @@ -39,7 +39,9 @@ Scrabble.highestScoreFrom = function(arrayOfWords) { } // if the scores are tied, check to see if one of the words is longer than 7 letters. if neither is, continue on as if nothing happened (this will effectively choose the first tied score to be the winner) // else if (scoreArray[i] == highestWordScore) { - if (highestWord.length < 7 && arrayOfWords[i].length >= 7) { + if (highestWord.length > arrayOfWords[i].length) { + highestWord = arrayOfWords[i]; + } else if (highestWord.length < 7 && arrayOfWords[i].length >= 7) { highestWordScore = scoreArray[i] highestWord = arrayOfWords[i]; }; @@ -49,24 +51,6 @@ Scrabble.highestScoreFrom = function(arrayOfWords) { return highestWord }; -// console.log(Scrabble.helloWorld()) -// // testing for score fx// regular word -// console.log("Score for 'word' is: " + Scrabble.score("word")) -// // bingo word (7+ letters) -// console.log("Score for 'foooooooblah' is: " + Scrabble.score("foooooooblah")) -// -// // testing highestScoreFrom fx -// console.log("hiii: " + Scrabble.score("hiii") + ", bloop: " + Scrabble.score("bloop") + ", cat: " + Scrabble.score("cat") ) -// console.log("highest score word is: " + Scrabble.highestScoreFrom(["hiii", "bloop", "cat"])) -// -// // test that in the case of an equal score, the word that is 7 letters or greater should be the winner -// console.log("zzzzzz: " + Scrabble.score("zzzzzz") + ", iiiiiif: " + Scrabble.score("iiiiiif")) -// console.log("highest score word is: " + Scrabble.highestScoreFrom(["zzzzzz", "iiiiiif"])) -// -// // test that in the case of an equal score where none is 7 letters, the first should be the winner -// console.log("aaaaaa: " + Scrabble.score("aaaaaa") + ", iiiiii: " + Scrabble.score("iiiiii")) -// console.log("highest score word is: " + Scrabble.highestScoreFrom(["aaaaaa", "iiiiii"])) -// // -// +// hashtag test-driven development. check out /spec folder to see testing on this module. // export this file as a module module.exports = Scrabble; diff --git a/scrabble_constants.js b/scrabble_constants.js index b91701b..10a9fd2 100644 --- a/scrabble_constants.js +++ b/scrabble_constants.js @@ -1,8 +1,10 @@ var SCRABBLE_CONSTANTS = { - SCORES: {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}, + SCORES: {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}, LETTERS: [["A", "A", "A", "A", "A", "A", "A", "A", "A"], ["B", "B"], ["C", "C"], From 4e90a633e3344c0dda8b416d46b386cf59e77cb6 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 14:25:08 -0800 Subject: [PATCH 16/19] tests for scrabble.helloworld, .highestscorefrom, .score --- spec/scrabble_spec.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index e69de29..081aeb7 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -0,0 +1,39 @@ +var Scrabble = require('../scrabble'); +var Player = require('../player'); + +describe('testing Scrabble module', function() { + + it('Scrabble can say hello', function() { + expect(Scrabble.helloWorld()).toBe("hello world!"); + }); + + it('scrabble can score a word correctly', function() { + expect(Scrabble.score("word")).toBe(8); + }); + + it('scrabble can score a bingo correctly', function() { + expect(Scrabble.score("uuuuuuu")).toBe(57); + }); + + it('scrabble can score a list of words, none of which are bingos, correctly', function() { + expect(Scrabble.highestScoreFrom(["uuuuuu", "you", "cat", "syzygy" ])).toBe("syzygy"); + }); + + it('scrabble can score a list of words, one of which is a bingo, correctly', function() { + expect(Scrabble.highestScoreFrom(["uuuuuua", "you", "cat", "syzygy" ])).toBe("uuuuuua"); + }); + + it('scrabble can score a list of words that are tied for score and length & choose the first one as the winner', function() { + expect(Scrabble.highestScoreFrom(["uuuuuu", "iiiiii", "aaaaaa" ])).toBe("uuuuuu"); + }); + + it('scrabble can score a list of words that are tied for score, except one is a bingo, choose the bingo rather than the one that is shorter', function() { + expect(Scrabble.highestScoreFrom(["zzzzzz", "iiiiiif" ])).toBe("iiiiiif"); + }); + + it('list of words tied for score but not length is scored correctly, where the shortest length one is returned if nothing is a bingo', function() { + expect(Scrabble.highestScoreFrom(["aaa", "c", "id" ])).toBe("c"); + }); + + +}); From 1d0a53fedb8aea032a8bc4a85c21b9f9d270f4a2 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 14:32:05 -0800 Subject: [PATCH 17/19] error handling for non-string arguments in .score --- scrabble.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/scrabble.js b/scrabble.js index 4a3a7e7..8cff746 100644 --- a/scrabble.js +++ b/scrabble.js @@ -10,15 +10,20 @@ Scrabble.helloWorld = function() { // add score method to Scrabble Scrabble.score = function(word){ - word = word.toUpperCase(); - var total = 0; - for (let char of word) { - total += CONSTANTS.SCORES[char] - }; - if (word.length >= 7) { - total += 50 + if (typeof(word) == 'string') { + word = word.toUpperCase(); + var total = 0; + for (let char of word) { + total += CONSTANTS.SCORES[char] + }; + if (word.length >= 7) { + total += 50 + }; + return total; + } else { + return false; }; - return total; + } Scrabble.highestScoreFrom = function(arrayOfWords) { From 03124204babe6f4636bcaa80f3d976acdc5324ad Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 20:53:50 -0800 Subject: [PATCH 18/19] add error handling in scrabble --- scrabble.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8cff746..72392d1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -10,7 +10,9 @@ Scrabble.helloWorld = function() { // add score method to Scrabble Scrabble.score = function(word){ - if (typeof(word) == 'string') { + + // if the word is a string and it does not contain any non-letter characters, then score it. else, return false. + if (typeof(word) == 'string' && Scrabble.checkInput(word)) { word = word.toUpperCase(); var total = 0; for (let char of word) { @@ -33,7 +35,13 @@ Scrabble.highestScoreFrom = function(arrayOfWords) { // iterate through words, score them all for (let word of arrayOfWords) { - scoreArray.push(Scrabble.score(word)); + // if the words aren't strings, fuhgeddaboutit + if (typeof(word) == 'string') { + scoreArray.push(Scrabble.score(word)); + } else { + // turns out we need this so that scoreArray.length == arrayOfWords.length + scoreArray.push(false); + }; }; // iterate through scores, find best based on conditional @@ -53,9 +61,22 @@ Scrabble.highestScoreFrom = function(arrayOfWords) { }; }; - return highestWord + if (highestWord) { + return highestWord; + } else { + return "There is no highest scoring word." + }; + }; +Scrabble.checkInput = function(word) { + if (word == word.match(/^[a-zA-Z]+$/)) { + return word + } else { + return false + } +} + // hashtag test-driven development. check out /spec folder to see testing on this module. // export this file as a module module.exports = Scrabble; From 6904fe7554a95eec42ee490a19f04f0fb4b4e6d9 Mon Sep 17 00:00:00 2001 From: laurenfb Date: Thu, 17 Nov 2016 20:54:02 -0800 Subject: [PATCH 19/19] tests for error handling --- spec/scrabble_spec.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 081aeb7..63c45e4 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -3,10 +3,13 @@ var Player = require('../player'); describe('testing Scrabble module', function() { + +////////////// easy test :) ////////////////////// it('Scrabble can say hello', function() { expect(Scrabble.helloWorld()).toBe("hello world!"); }); +/////////////////// score ////////////////////////// it('scrabble can score a word correctly', function() { expect(Scrabble.score("word")).toBe(8); }); @@ -15,6 +18,17 @@ describe('testing Scrabble module', function() { expect(Scrabble.score("uuuuuuu")).toBe(57); }); + it('scrabble returns false when input is not a string', function() { + expect(Scrabble.score(23948)).toBe(false); + expect(Scrabble.score([3,5, "cat"])).toBe(false); + }); + + it('if a word does not have only letters, scrabble returns false', function() { + expect(Scrabble.score("23948cat")).toBe(false); + expect(Scrabble.score("cat0%")).toBe(false); + }); + +/////////// highestScoreFrom working correctly //////////// it('scrabble can score a list of words, none of which are bingos, correctly', function() { expect(Scrabble.highestScoreFrom(["uuuuuu", "you", "cat", "syzygy" ])).toBe("syzygy"); }); @@ -35,5 +49,17 @@ describe('testing Scrabble module', function() { expect(Scrabble.highestScoreFrom(["aaa", "c", "id" ])).toBe("c"); }); +//////////// highestScoreFrom error testing ///////// + it('when passed an empty array, highestScoreFrom should let you know', function() { + expect(Scrabble.highestScoreFrom([])).toBe("There is no highest scoring word."); + }); + + it('when passed an array of garbage, highestScoreFrom should let you know', function() { + expect(Scrabble.highestScoreFrom(["dsfk24234", "$%#;", 230585])).toBe("There is no highest scoring word."); + }); + + it('when passed an array where one word is valid and the rest are garbage, it should score that word & return it as the highest scoring', function() { + expect(Scrabble.highestScoreFrom(["dsfk24234", "$%#;", 230585, "cat"])).toBe("cat"); + }); });