From c9de1cb29004ed1e7754d32e620811b113c1778a Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Wed, 16 Nov 2016 12:14:50 -0800 Subject: [PATCH 1/6] implemented score(word) function correctly --- scrabble.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index a7d0745..a4a0ee9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,4 +1,40 @@ -var Scrabble = function() {}; +var Scrabble = function() { + this.scoreTable = [ + [1, "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], + [2, "D", "G"], + [3, "B", "C", "M", "P"], + [4, "F", "H", "V", "W", "Y"], + [5, "K"], + [8, "J", "X"], + [10, "Q", "Z"] + ]; +}; + +// score(word): returns the total score value for the given word. The word is input as a string (case insensitive). +Scrabble.prototype.score = function(word) { + var wordScore = 0; + var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because 'this' scope changes inside anonymous functions. + var wordAsArray = word.toUpperCase().split(""); + wordAsArray.forEach(function(letter) { + scoreTable.forEach(function(scoreRow) { + if (scoreRow.includes(letter)) { + wordScore += scoreRow[0]; + } + }); + }); + return wordScore; +}; + +var testing = new Scrabble(); +console.log(testing.score("suzannah")); + +// score(word): returns the total score value for the given word. The word is input as a string (case insensitive). The chart below shows the point value for a given letter. + + +Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + +}; +// highestScoreFrom(arrayOfWords): returns the word // YOUR CODE HERE Scrabble.prototype.helloWorld = function() { @@ -6,3 +42,14 @@ Scrabble.prototype.helloWorld = function() { }; module.exports = Scrabble; + + +// this.scoreTable = { +// 1: [1, "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], +// 2: [2, "D", "G"], +// 3: [3, "B", "C", "M", "P"], +// 4: [4, "F", "H", "V", "W", "Y"], +// 5: [5, "K"], +// 6: [8, "J", "X"], +// 7: [10, "Q", "Z"] +// }; From 87392981622927d0b1d75ba33c0d1f33062676d8 Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Wed, 16 Nov 2016 18:17:38 -0800 Subject: [PATCH 2/6] implemented highestScoreFrom function accurately via manual tests, completing WAVE ONE requirements --- scrabble.js | 79 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/scrabble.js b/scrabble.js index a4a0ee9..1843fc9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -12,8 +12,11 @@ var Scrabble = function() { // score(word): returns the total score value for the given word. The word is input as a string (case insensitive). Scrabble.prototype.score = function(word) { + if (word.length > 7) { + return 0; + } var wordScore = 0; - var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because 'this' scope changes inside anonymous functions. + var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because the scope of 'this' changes inside anonymous functions. var wordAsArray = word.toUpperCase().split(""); wordAsArray.forEach(function(letter) { scoreTable.forEach(function(scoreRow) { @@ -25,31 +28,75 @@ Scrabble.prototype.score = function(word) { return wordScore; }; -var testing = new Scrabble(); -console.log(testing.score("suzannah")); +// highestScoreFrom(arrayOfWords): returns the word in the array with the highest score (based on specs). +Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + var scores = []; + for (var i = 0; i < arrayOfWords.length; i++) { + aScore = this.score(arrayOfWords[i]); + scores.push(aScore); + } -// score(word): returns the total score value for the given word. The word is input as a string (case insensitive). The chart below shows the point value for a given letter. + // find the highest score + var greatestVal = 0; + for (var j = 0; j < scores.length; j++) { + if (scores[j] > greatestVal) { + greatestVal = scores[j]; + } + } + // to handle "tie" situations, create a new array with the words whose score is the greatestVal + var highestScoreWords = []; + for (var k = 0; k < arrayOfWords.length; k++) { + if (this.score(arrayOfWords[k]) === greatestVal) { + highestScoreWords.push(arrayOfWords[k]); + } + } + console.log(highestScoreWords); // this line is solely for internal testing purposes to confirm everything below is working as expected. Keeping live because it makes my tests below easy to confirm. -Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + if (highestScoreWords.length === 1) { + return highestScoreWords[0]; + } else { + var minLength = 7; + for (var l = 0; l < highestScoreWords.length; l++) { + if (highestScoreWords[l].length === 7) { + return highestScoreWords[l]; + } else if (highestScoreWords[l].length < minLength) { + minLength = highestScoreWords[l].length; + } + } + for (var m = 0; m < highestScoreWords.length; m++) { + if (highestScoreWords[m].length === minLength) { + return highestScoreWords[m]; + } + } + } }; -// highestScoreFrom(arrayOfWords): returns the word -// YOUR CODE HERE + + + +var testing = new Scrabble(); + + +// YOUR CODE HERE - these next 4 lines part of the originial file Scrabble.prototype.helloWorld = function() { return 'hello world!'; }; +// TESTING... (see comments for expected outputs) +// var testing = new Scrabble(); +console.log(testing.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. +console.log(testing.score("quiz")); // 22 +console.log(testing.score("quizlets")); // 0, because the word's length is more than 7. +console.log(testing.highestScoreFrom(["tea", "coffee", "qqqq"])); // qqqq - when there's only one highest scoring word, this is the clear winner. +console.log(testing.highestScoreFrom(["tea", "coffee", "qqqq", "qqqkdda"])); // qqqkdda - when multiple words have the same highest score, the (first) word in the input array that is 7-letters wins. +console.log(testing.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"])); // zzzz - when multiple words have the same highest score and none of them are 7-letters, the (first) highest scoring word with the fewest letters wins. + + module.exports = Scrabble; -// this.scoreTable = { -// 1: [1, "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], -// 2: [2, "D", "G"], -// 3: [3, "B", "C", "M", "P"], -// 4: [4, "F", "H", "V", "W", "Y"], -// 5: [5, "K"], -// 6: [8, "J", "X"], -// 7: [10, "Q", "Z"] -// }; + +// Items I have not done in this project's short life: + // Assuming all inputs are alpha characters From 0f8d35c77c5b2558111389e67d88c649eb6da247 Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Wed, 16 Nov 2016 22:13:49 -0800 Subject: [PATCH 3/6] completed WAVE 2--larger git commit than usual, oops. I did a lot and got in a flow. Many of these object properties interacted with others, so I made them somewhat in tandem. --- scrabble.js | 86 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1843fc9..926782b 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,4 +1,5 @@ var Scrabble = function() { + // due to how the specs are written, this score table does not account for the 50 point bonus for 7-letter words... this.scoreTable = [ [1, "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], [2, "D", "G"], @@ -10,6 +11,9 @@ var Scrabble = function() { ]; }; +// this is used for testing throughout and below... +var testingS = new Scrabble(); + // score(word): returns the total score value for the given word. The word is input as a string (case insensitive). Scrabble.prototype.score = function(word) { if (word.length > 7) { @@ -44,14 +48,14 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { } } - // to handle "tie" situations, create a new array with the words whose score is the greatestVal + // to handle "tie" situations, creating a new array with the words whose score is the greatestVal var highestScoreWords = []; for (var k = 0; k < arrayOfWords.length; k++) { if (this.score(arrayOfWords[k]) === greatestVal) { highestScoreWords.push(arrayOfWords[k]); } } - console.log(highestScoreWords); // this line is solely for internal testing purposes to confirm everything below is working as expected. Keeping live because it makes my tests below easy to confirm. + // console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' if doing future testing. if (highestScoreWords.length === 1) { return highestScoreWords[0]; @@ -75,23 +79,81 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { +var Player = function(name) { + this.name = name.toUpperCase(); + this.plays = []; +}; + +// this is used for testing throughout and below... +var testingP = new Player("suzannah"); + +Player.prototype.totalScore = function() { + var playerScore = 0; + for (var n = 0; n < this.plays.length; n++) { + playerScore += testingS.score(this.plays[n]); + } + return playerScore; +}; + +Player.prototype.hasWon = function() { + if (this.totalScore() > 100) { + return true; + } else { + return false; + } +}; + +Player.prototype.play = function(word) { + if (this.totalScore() > 100) { + return false; + } else { + this.plays.push(word.toUpperCase()); + } +}; + +Player.prototype.highestScoringWord = function() { + return testingS.highestScoreFrom(this.plays); +}; + +Player.prototype.highestWordScore = function() { + return testingS.score(this.highestScoringWord()); +}; -var testing = new Scrabble(); -// YOUR CODE HERE - these next 4 lines part of the originial file +// YOUR CODE HERE - this and the next 4 lines part of the original file Scrabble.prototype.helloWorld = function() { return 'hello world!'; }; -// TESTING... (see comments for expected outputs) -// var testing = new Scrabble(); -console.log(testing.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. -console.log(testing.score("quiz")); // 22 -console.log(testing.score("quizlets")); // 0, because the word's length is more than 7. -console.log(testing.highestScoreFrom(["tea", "coffee", "qqqq"])); // qqqq - when there's only one highest scoring word, this is the clear winner. -console.log(testing.highestScoreFrom(["tea", "coffee", "qqqq", "qqqkdda"])); // qqqkdda - when multiple words have the same highest score, the (first) word in the input array that is 7-letters wins. -console.log(testing.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"])); // zzzz - when multiple words have the same highest score and none of them are 7-letters, the (first) highest scoring word with the fewest letters wins. + + +// TESTING SCRABBLE... (see comments for expected outputs) +console.log(testingS.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. +console.log(testingS.score("quiz")); // 22 +console.log(testingS.score("quizlets")); // 0, because the word's length is more than 7. +console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq"])); // qqqq - when there's only one highest scoring word, this is the clear winner. +console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq", "qqqkdda"])); // qqqkdda - when multiple words have the same highest score, the (first) word in the input array that is 7-letters wins. +console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"])); // zzzz - when multiple words have the same highest score and none of them are 7-letters, the (first) highest scoring word with the fewest letters wins. + + + +// TESTING PLAYER... (see comments for expected outputs) +console.log(testingP.name); // SUZANNAH +testingP.play("zebras"); +testingP.play("giraffe"); +console.log(testingP.plays); // [ 'ZEBRA', 'GIRAFFE' ] +console.log(testingP.totalScore()); // 31 +console.log(testingP.hasWon()); // false +testingP.play("qqqqqqq"); +console.log(testingP.plays); // [ 'ZEBRAS', 'GIRAFFE', 'QQQQQQQ' ] +console.log(testingP.totalScore()); // 101 +console.log(testingP.hasWon()); // true +console.log(testingP.play("is")); // false - because this player has already won. +console.log(testingP.plays); // [ 'ZEBRAS', 'GIRAFFE', 'QQQQQQQ' ] - simply confirming that nothing was added into the plays array from the line above. +console.log(testingP.highestScoringWord()); // QQQQQQQ +console.log(testingP.highestWordScore()); // 70 + module.exports = Scrabble; From 4ad41b70c6951b9e16e701038f87fddc877ee854 Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Thu, 17 Nov 2016 09:57:26 -0800 Subject: [PATCH 4/6] fixed the player object so that each player will refer to its own scrabble game object, not the testing game object as I coded last night --- scrabble.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scrabble.js b/scrabble.js index 926782b..422810a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -11,9 +11,6 @@ var Scrabble = function() { ]; }; -// this is used for testing throughout and below... -var testingS = new Scrabble(); - // score(word): returns the total score value for the given word. The word is input as a string (case insensitive). Scrabble.prototype.score = function(word) { if (word.length > 7) { @@ -55,7 +52,8 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { highestScoreWords.push(arrayOfWords[k]); } } - // console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' if doing future testing. + + // console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' for testing in the future if doing tweaks on this object. if (highestScoreWords.length === 1) { return highestScoreWords[0]; @@ -82,15 +80,13 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { var Player = function(name) { this.name = name.toUpperCase(); this.plays = []; + this.game = new Scrabble(); }; -// this is used for testing throughout and below... -var testingP = new Player("suzannah"); - Player.prototype.totalScore = function() { var playerScore = 0; for (var n = 0; n < this.plays.length; n++) { - playerScore += testingS.score(this.plays[n]); + playerScore += this.game.score(this.plays[n]); } return playerScore; }; @@ -112,11 +108,11 @@ Player.prototype.play = function(word) { }; Player.prototype.highestScoringWord = function() { - return testingS.highestScoreFrom(this.plays); + return this.game.highestScoreFrom(this.plays); }; Player.prototype.highestWordScore = function() { - return testingS.score(this.highestScoringWord()); + return this.game.score(this.highestScoringWord()); }; @@ -129,6 +125,7 @@ Scrabble.prototype.helloWorld = function() { // TESTING SCRABBLE... (see comments for expected outputs) +var testingS = new Scrabble(); console.log(testingS.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. console.log(testingS.score("quiz")); // 22 console.log(testingS.score("quizlets")); // 0, because the word's length is more than 7. @@ -139,6 +136,7 @@ console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"] // TESTING PLAYER... (see comments for expected outputs) +var testingP = new Player('suzannah'); console.log(testingP.name); // SUZANNAH testingP.play("zebras"); testingP.play("giraffe"); @@ -160,5 +158,5 @@ module.exports = Scrabble; -// Items I have not done in this project's short life: - // Assuming all inputs are alpha characters +// Notes about this project's short life: + // I'm assuming all inputs are alpha characters From d03f3b61cd848a6ffb552ff867537fed8ff378dc Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Fri, 18 Nov 2016 13:55:45 -0800 Subject: [PATCH 5/6] whew! found a work-around to get my alphaChars validation to work given my forEach functions. --- scrabble.js | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/scrabble.js b/scrabble.js index 422810a..3ba9873 100644 --- a/scrabble.js +++ b/scrabble.js @@ -13,20 +13,33 @@ var Scrabble = function() { // score(word): returns the total score value for the given word. The word is input as a string (case insensitive). Scrabble.prototype.score = function(word) { + var wordScore = 0; + var nonAlphaChar = 0; + var alphaChars = ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T", "D", "G", "B", "C", "M", "P", "F", "H", "V", "W", "Y", "K", "J", "X", "Q", "Z"]; + + var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because the scope of 'this' changes inside anonymous functions. + if (word.length > 7) { return 0; } - var wordScore = 0; - var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because the scope of 'this' changes inside anonymous functions. + var wordAsArray = word.toUpperCase().split(""); wordAsArray.forEach(function(letter) { - scoreTable.forEach(function(scoreRow) { - if (scoreRow.includes(letter)) { - wordScore += scoreRow[0]; - } - }); + if (alphaChars.includes(letter)) { + scoreTable.forEach(function(scoreRow) { + if (scoreRow.includes(letter)) { + wordScore += scoreRow[0]; + } + }); + } else if (alphaChars.includes(letter) === false) { + nonAlphaChar += 1; // this is my second or fourth idea how to handle words with non-alpha characters, since the forEach function behaves differently than I originally expected. + } }); - return wordScore; + if (nonAlphaChar !== 0) { + return 0; + } else { + return wordScore; + } }; // highestScoreFrom(arrayOfWords): returns the word in the array with the highest score (based on specs). @@ -53,7 +66,7 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { } } - // console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' for testing in the future if doing tweaks on this object. + // console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' for testing in the future if doing tweaks on this object. if (highestScoreWords.length === 1) { return highestScoreWords[0]; @@ -129,6 +142,9 @@ var testingS = new Scrabble(); console.log(testingS.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. console.log(testingS.score("quiz")); // 22 console.log(testingS.score("quizlets")); // 0, because the word's length is more than 7. +console.log(testingS.score("quiz1")); // 0, because one character is not A-Z. +console.log(testingS.score("quiz ")); // 0, because one character is not A-Z. +console.log(testingS.score("quiz*")); // 0, because one character is not A-Z. console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq"])); // qqqq - when there's only one highest scoring word, this is the clear winner. console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq", "qqqkdda"])); // qqqkdda - when multiple words have the same highest score, the (first) word in the input array that is 7-letters wins. console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"])); // zzzz - when multiple words have the same highest score and none of them are 7-letters, the (first) highest scoring word with the fewest letters wins. @@ -160,3 +176,11 @@ module.exports = Scrabble; // Notes about this project's short life: // I'm assuming all inputs are alpha characters + + +// DRAFTING PULL REQUEST... + // This was a great, short project to get more familiar with JS syntax. Last night I was so grateful for console.log helping me "see" that I'd forgotten () and that's why my code wasn't executing how I expected it to. + // + // This code is not DRY. + // + // Also, I am assuming all inputs are alpha characters. I know how I would check for this in a non-DRY way. From 423728f86a256025d042112ca08ac93fa00c71aa Mon Sep 17 00:00:00 2001 From: Suzannah Kirk-Daligcon Date: Fri, 18 Nov 2016 13:56:41 -0800 Subject: [PATCH 6/6] clean-up. Deleting notes that will live in this project's pull request. --- scrabble.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index 3ba9873..5183b2a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -171,16 +171,3 @@ console.log(testingP.highestWordScore()); // 70 module.exports = Scrabble; - - - -// Notes about this project's short life: - // I'm assuming all inputs are alpha characters - - -// DRAFTING PULL REQUEST... - // This was a great, short project to get more familiar with JS syntax. Last night I was so grateful for console.log helping me "see" that I'd forgotten () and that's why my code wasn't executing how I expected it to. - // - // This code is not DRY. - // - // Also, I am assuming all inputs are alpha characters. I know how I would check for this in a non-DRY way.