From 978438f1bb8cdb35e111fb408bb55ddc7fe94df8 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Tue, 15 May 2018 17:03:21 -0700 Subject: [PATCH 01/10] Working on score instance method with in Scrabble Object --- scrabble.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index e95f352..4898088 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,36 @@ - +//A, E, I, O, U, L, N, R, S, T 1 +// D, G 2 +// B, C, M, P 3 +// F, H, V, W, Y 4 +// K 5 +// J, X 8 +// Q, Z 10 const Scrabble = { + const scoreBoard = { + '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 + }; + score(word) { + // split our word into an array of characters + let wordArray = word.split(""); + // if the word <= 7 in length proceed + while (wordArray.length <= 7) { + // look at each letter in the array and scoreboard and then reassign value at index to value from scoreBoard wordArray[i] = scoreBoard[wordArray[i]] + for (let i = 0; i < wordArray.length; i++;) { + wordArray[i] = scoreBoard[wordArray[i]] + // TODO: return a sum of this array now that values have been assigned + } + }); }, + + highestScoreFrom(arrayOfWords) { }, From 43eea8829775e30dff21fa8cc94110ac14b3ec04 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Tue, 15 May 2018 18:09:26 -0700 Subject: [PATCH 02/10] Added in a summing feature for score instance function --- scrabble.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index 4898088..136af6e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -25,7 +25,7 @@ const Scrabble = { for (let i = 0; i < wordArray.length; i++;) { wordArray[i] = scoreBoard[wordArray[i]] // TODO: return a sum of this array now that values have been assigned - + return wordArray.reduce((x, y) => x + y); } }); }, From a864665c9871699cd82213a3cf125b9dba22be76 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Wed, 16 May 2018 14:23:39 -0700 Subject: [PATCH 03/10] correctly scores simple words test passing --- package.json | 2 +- scrabble.js | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 50444a7..ec337f6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "license": "ISC", "dependencies": { "fs": "0.0.1-security", - "npm": "^5.8.0" + "npm": "^5.10.0" }, "scripts": { "test": "jest" diff --git a/scrabble.js b/scrabble.js index 136af6e..112057c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -6,7 +6,7 @@ // J, X 8 // Q, Z 10 const Scrabble = { - const scoreBoard = { + scoreBoard: { '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, @@ -14,23 +14,25 @@ const Scrabble = { 'K': 5, 'J': 8, 'X': 8, 'Q': 10, 'Z': 10 - }; + }, score(word) { + let totalScore = 0; // split our word into an array of characters - let wordArray = word.split(""); + let upcase_word = word.toUpperCase() + let wordArray = upcase_word.split(""); // if the word <= 7 in length proceed while (wordArray.length <= 7) { // look at each letter in the array and scoreboard and then reassign value at index to value from scoreBoard wordArray[i] = scoreBoard[wordArray[i]] - for (let i = 0; i < wordArray.length; i++;) { - wordArray[i] = scoreBoard[wordArray[i]] + for (let i = 0; i < wordArray.length; i++) { + wordArray[i] = Scrabble.scoreBoard[wordArray[i]] // TODO: return a sum of this array now that values have been assigned - return wordArray.reduce((x, y) => x + y); + totalScore = wordArray.reduce((x, y) => x + y); } - }); + return totalScore + } }, - highestScoreFrom(arrayOfWords) { }, From 7e6786360ab1c91bbf07c3b503e9dfe53204634f Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Wed, 16 May 2018 14:34:44 -0700 Subject: [PATCH 04/10] test passing for 7 letter word bonus 50 points, and handling all upper and lower case conditions --- scrabble.js | 11 ++++++++--- specs/scrabble.spec.js | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index 112057c..35535b1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -18,7 +18,7 @@ const Scrabble = { score(word) { let totalScore = 0; - // split our word into an array of characters + // split our word into an array of Uppercase characters let upcase_word = word.toUpperCase() let wordArray = upcase_word.split(""); // if the word <= 7 in length proceed @@ -26,10 +26,15 @@ const Scrabble = { // look at each letter in the array and scoreboard and then reassign value at index to value from scoreBoard wordArray[i] = scoreBoard[wordArray[i]] for (let i = 0; i < wordArray.length; i++) { wordArray[i] = Scrabble.scoreBoard[wordArray[i]] - // TODO: return a sum of this array now that values have been assigned + //gets a sum of this array now that values have been assigned totalScore = wordArray.reduce((x, y) => x + y); } - return totalScore + // Adds 50 points if you play a 7 letter word, else just returns your normal score + if (wordArray.length === 7) { + return totalScore + 50; + } else { + return totalScore; + } } }, diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b562037..0f3a360 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -11,7 +11,7 @@ describe('score', () => { expect(Scrabble.score('pig')).toBe(6); }); - test.skip('adds 50 points for a 7-letter word', () => { + test('adds 50 points for a 7-letter word', () => { expect(Scrabble.score('academy')).toBe(65); }); @@ -21,7 +21,7 @@ describe('score', () => { }).toThrow(); }); - test.skip('handles all upper- and lower-case letters', () => { + test('handles all upper- and lower-case letters', () => { expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('DOG')).toBe(5); expect(Scrabble.score('DoG')).toBe(5); From 1539ecf3f988725a1a73c0ec0b0ea5d1904c099b Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Wed, 16 May 2018 15:07:45 -0700 Subject: [PATCH 05/10] Tests for throw conditions for score instance method are passing --- scrabble.js | 12 ++++++++++++ specs/scrabble.spec.js | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 35535b1..20d1f04 100644 --- a/scrabble.js +++ b/scrabble.js @@ -18,17 +18,29 @@ const Scrabble = { score(word) { let totalScore = 0; + + // throws if word contains invalid characters + // .search will return -1 if there is a match + // throws if word is empty + //throws if word is longer then 7 characters + if (word.search(/[^a-zA-Z]+/) !== -1 || word.length === 0 || word.length > 7) { + throw 'Invalid word.'; + } + // split our word into an array of Uppercase characters let upcase_word = word.toUpperCase() let wordArray = upcase_word.split(""); + // if the word <= 7 in length proceed while (wordArray.length <= 7) { + // look at each letter in the array and scoreboard and then reassign value at index to value from scoreBoard wordArray[i] = scoreBoard[wordArray[i]] for (let i = 0; i < wordArray.length; i++) { wordArray[i] = Scrabble.scoreBoard[wordArray[i]] //gets a sum of this array now that values have been assigned totalScore = wordArray.reduce((x, y) => x + y); } + // Adds 50 points if you play a 7 letter word, else just returns your normal score if (wordArray.length === 7) { return totalScore + 50; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 0f3a360..b19511d 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -15,7 +15,7 @@ describe('score', () => { expect(Scrabble.score('academy')).toBe(65); }); - test.skip('throws on bad characters', () => { + test('throws on bad characters', () => { expect(() => { Scrabble.score('char^'); }).toThrow(); @@ -27,11 +27,11 @@ describe('score', () => { expect(Scrabble.score('DoG')).toBe(5); }); - test.skip('does not allow words > 7 letters', () => { + test('does not allow words > 7 letters', () => { expect(() => { Scrabble.score('abcdefgh'); }).toThrow(); }); - test.skip('does not allow empty words', () => { + test('does not allow empty words', () => { expect(() => { Scrabble.score(''); }).toThrow(); }); }); From c43385f211f6a01937d15750ac1511199e3f5b46 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Wed, 16 May 2018 17:04:01 -0700 Subject: [PATCH 06/10] Highestscore tests in Progress some passing --- scrabble.js | 24 +++++++++++++++++++++++- specs/scrabble.spec.js | 8 ++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index 20d1f04..8d3869d 100644 --- a/scrabble.js +++ b/scrabble.js @@ -51,8 +51,30 @@ const Scrabble = { }, highestScoreFrom(arrayOfWords) { + // throw if no words are passed + if (arrayOfWords.length === 0 || Array.isArray(arrayOfWords) !== true) { + throw "No played words."; + } - }, + let bestWord = arrayOfWords[0]; + let bestScore = this.score(bestWord); + let tiedWords = [] + + // Need FXN for time break on tiedWords array + // tieBreak() + + + arrayOfWords.forEach((word) => { + if (this.score(word) > bestScore) { + bestWord = word; + bestScore = this.score(word); + // } else if (this.score(word) === bestScore) { + // bestWord = tieBreak(bestWord, word) + // } + } + }) + return bestWord + } }; Scrabble.Player = class { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b19511d..1db8fd3 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -37,20 +37,20 @@ describe('score', () => { }); describe('highestScoreFrom', () => { - test.skip('is defined', () => { + test('is defined', () => { expect(Scrabble.highestScoreFrom).toBeDefined(); }); - test.skip('throws if no words were passed', () => { + test('throws if no words were passed', () => { expect(() => { Scrabble.highestScoreFrom([]); }).toThrow(); expect(() => { Scrabble.highestScoreFrom('not array'); }).toThrow(); }); - test.skip('returns the only word in a length-1 array', () => { + test('returns the only word in a length-1 array', () => { expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); }); - test.skip('returns the highest word if there are two words', () => { + test('returns the highest word if there are two words', () => { // Check score assumptions expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('pig')).toBe(6); From a93c42a554ebe425f7d6129f3025d8bc3173573d Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Thu, 17 May 2018 20:04:47 -0700 Subject: [PATCH 07/10] All tests working except one, though test case is included in code. Ask tomorrow. --- scrabble.js | 24 ++++++++++++++++-------- specs/scrabble.spec.js | 6 +++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8d3869d..567690e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -55,22 +55,30 @@ const Scrabble = { if (arrayOfWords.length === 0 || Array.isArray(arrayOfWords) !== true) { throw "No played words."; } + + // FIXME: The test for returning a 7 letter word in the result of a tie is not working despite test case seeming to be covered below + // FXN for time break on tiedWords array + const tieBreak = function tieBreak(best_word, challenger_word) { + if (best_word.length === 7) { + return best_word; + } else if (challenger_word === 7) { + return challenger_word; + } else if (challenger_word.length < best_word.length) { + return challenger_word; + } else { + return best_word + } + }; let bestWord = arrayOfWords[0]; let bestScore = this.score(bestWord); - let tiedWords = [] - - // Need FXN for time break on tiedWords array - // tieBreak() - arrayOfWords.forEach((word) => { if (this.score(word) > bestScore) { bestWord = word; bestScore = this.score(word); - // } else if (this.score(word) === bestScore) { - // bestWord = tieBreak(bestWord, word) - // } + } else if (this.score(word) === bestScore) { + bestWord = tieBreak(bestWord, word); } }) return bestWord diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 1db8fd3..5c05066 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -60,7 +60,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); }); - test.skip('if tied, prefer a word with 7 letters', () => { + test('if tied, prefer a word with 7 letters', () => { const loser = 'zzzzzz'; const winner = 'iiiiddd'; @@ -73,7 +73,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); }); - test.skip('if tied and no word has 7 letters, prefers the word with fewer letters', () => { + test('if tied and no word has 7 letters, prefers the word with fewer letters', () => { // Check score assumptions expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('goat')).toBe(5); @@ -83,7 +83,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); }); - test.skip('returns the first word of a tie with same letter count', () => { + test('returns the first word of a tie with same letter count', () => { // Check score assumptions expect(Scrabble.score('i')).toBe(1); expect(Scrabble.score('dog')).toBe(5); From 31b14c50347b60c153a08549fea0cd2935bc333a Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Thu, 17 May 2018 22:43:45 -0700 Subject: [PATCH 08/10] Too tired to continue right now, will finish final two methods for Player class and associated tests tomorrow morning --- scrabble.js | 46 +++++++++++++++++++++++++++++++++++++++++- specs/scrabble.spec.js | 40 +++++++++++++++++++++--------------- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/scrabble.js b/scrabble.js index 567690e..662999d 100644 --- a/scrabble.js +++ b/scrabble.js @@ -55,7 +55,7 @@ const Scrabble = { if (arrayOfWords.length === 0 || Array.isArray(arrayOfWords) !== true) { throw "No played words."; } - + // FIXME: The test for returning a 7 letter word in the result of a tie is not working despite test case seeming to be covered below // FXN for time break on tiedWords array const tieBreak = function tieBreak(best_word, challenger_word) { @@ -86,7 +86,51 @@ const Scrabble = { }; Scrabble.Player = class { + constructor(name) { + this.name = name; + if (arguments.length === 0) { + throw 'Must provide a name.'; + } + this.plays = []; + this.score = 0; + } + + play(word) { + let word_score = 0; + if (this.hasWon()) { + return false; + } + + if (word.search(/[^a-zA-Z]+/) !== -1 || word.length > 7) { + throw 'You must enter a real word that is 7 characters or under.'; + } + + if (this.plays.includes(word)) { + throw "This word was used already."; + } else { + this.plays.push(word); + word_score = Scrabble.score(word); + this.score += word_score + } + return word_score; + } + + totalScore() { + return this.score; + } + + hasWon() { + return this.score >= 100; + } + highestScoringWord() { + // FIXME: Need to implement the rest of these tests. Tests are currently not working + return Scrabble.highestWordFrom(this.plays); + } + + highestWordScore() { + + } }; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 5c05066..cd1ef88 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -98,19 +98,19 @@ describe('highestScoreFrom', () => { }); describe('Player', () => { - test.skip('is defined', () => { + test('is defined', () => { expect(Scrabble.Player).toBeDefined(); }); describe('Constructor', () => { - test.skip('Creates a new player', () => { + test('Creates a new player', () => { const name = 'test name'; const player = new Scrabble.Player(name); expect(player.name).toBe(name); }); - test.skip('Requires a name', () => { + test('Requires a name', () => { expect(() => { new Scrabble.Player(); }).toThrow(); @@ -118,7 +118,7 @@ describe('Player', () => { }); describe('play', () => { - test.skip('Records the played word', () => { + test('Records the played word', () => { const word = 'dog'; const player = new Scrabble.Player('test player'); @@ -130,7 +130,7 @@ describe('Player', () => { expect(player.plays[0]).toBe(word); }); - test.skip('Requires a real word', () => { + test('Requires a real word', () => { const player = new Scrabble.Player('test player'); expect(player.plays.length).toBe(0); @@ -142,7 +142,7 @@ describe('Player', () => { expect(player.plays.length).toBe(0); }); - test.skip('Returns false and does not update plays if the player has already won', () => { + test('Returns false and does not update plays if the player has already won', () => { const player = new Scrabble.Player('test player'); expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts @@ -155,13 +155,13 @@ describe('Player', () => { }); describe('totalScore', () => { - test.skip('Is zero if the player has not played anything', () => { + test('Is zero if the player has not played anything', () => { const player = new Scrabble.Player('test player'); expect(player.totalScore()).toBe(0); }); - test.skip('Is updated by play', () => { + test('Is updated by play', () => { // Arrange const player = new Scrabble.Player('test player'); const words = [{word: 'dog', score: 5}, {word: 'cat', score: 5}, {word: 'goat', score: 5}]; @@ -181,28 +181,36 @@ describe('Player', () => { }); describe('hasWon', () => { - test.skip('returns false when score < 100', () => { - + test('returns false when score < 100', () => { + const player = new Scrabble.Player('test player'); + player.score = 99 + expect(player.hasWon()).toBe(false); }); - test.skip('returns true when score == 100', () => { - + test('returns true when score == 100', () => { + const player = new Scrabble.Player('test player'); + player.score = 100 + expect(player.hasWon()).toBe(true); }); - test.skip('returns true when score > 100', () => { - + test('returns true when score > 100', () => { + const player = new Scrabble.Player('test player'); + player.score = 101 + expect(player.hasWon()).toBe(true); }); }); describe('highestScoringWord', () => { // Tie-breaking logic is already described in the tests // for highestWordFrom, so we will not repeat it here. - test.skip('returns the highest scoring word played', () => { - + test('returns the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + player.this.plays.push('mirror'); + expect(player.highestScoringWord()).toBe('mirror'); }); test.skip('throws an error if no words have been played', () => { From 42ab46c685fa81c11031a12816e7920c7d409163 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Fri, 18 May 2018 08:48:58 -0700 Subject: [PATCH 09/10] All methods but highestWordScore implemented and tested --- scrabble.js | 8 +++++--- specs/scrabble.spec.js | 8 ++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/scrabble.js b/scrabble.js index 662999d..8c7800a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -59,9 +59,11 @@ const Scrabble = { // FIXME: The test for returning a 7 letter word in the result of a tie is not working despite test case seeming to be covered below // FXN for time break on tiedWords array const tieBreak = function tieBreak(best_word, challenger_word) { + console.log(`Best Word: ${best_word}`); + console.log(`Challenger Word: ${challenger_word}`); if (best_word.length === 7) { return best_word; - } else if (challenger_word === 7) { + } else if (challenger_word.length === 7) { return challenger_word; } else if (challenger_word.length < best_word.length) { return challenger_word; @@ -124,8 +126,8 @@ Scrabble.Player = class { } highestScoringWord() { - // FIXME: Need to implement the rest of these tests. Tests are currently not working - return Scrabble.highestWordFrom(this.plays); + // FIXME: Need to implement the rest of these tests. Tests are currently not working highestScoreFrom + return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index cd1ef88..74efa18 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -208,24 +208,24 @@ describe('Player', () => { // for highestWordFrom, so we will not repeat it here. test('returns the highest scoring word played', () => { const player = new Scrabble.Player('test player'); - player.this.plays.push('mirror'); + player.play('mirror'); expect(player.highestScoringWord()).toBe('mirror'); }); - test.skip('throws an error if no words have been played', () => { + test('throws an error if no words have been played', () => { }); }); describe('highestWordScore', () => { - test.skip('returns the score of the highest scoring word played', () => { + test('returns the score of the highest scoring word played', () => { }); - test.skip('throws an error if no words have been played', () => { + test('throws an error if no words have been played', () => { }); From f74d95be4e06486fdf29c770a4a8e53d099f5e64 Mon Sep 17 00:00:00 2001 From: Ana Lisa Sutherland Date: Fri, 18 May 2018 09:01:52 -0700 Subject: [PATCH 10/10] Finishing notes added, removed comments for fixes --- scrabble.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8c7800a..e273b95 100644 --- a/scrabble.js +++ b/scrabble.js @@ -56,8 +56,6 @@ const Scrabble = { throw "No played words."; } - // FIXME: The test for returning a 7 letter word in the result of a tie is not working despite test case seeming to be covered below - // FXN for time break on tiedWords array const tieBreak = function tieBreak(best_word, challenger_word) { console.log(`Best Word: ${best_word}`); console.log(`Challenger Word: ${challenger_word}`); @@ -126,12 +124,11 @@ Scrabble.Player = class { } highestScoringWord() { - // FIXME: Need to implement the rest of these tests. Tests are currently not working highestScoreFrom return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { - + // ran out of time to implement and test this. } };