From cbf06cefb6019ea6023bed59d93d37dcab18cd4c Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Tue, 15 May 2018 15:57:01 -0700 Subject: [PATCH 01/12] wrote scoring function --- scrabble.js | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index e95f352..0f47201 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,11 +1,53 @@ +const LETTER_VALUES = + { + "a": { "points": 1, "tiles": 9 }, + "b": { "points": 3, "tiles": 2 }, + "c": { "points": 3, "tiles": 2 }, + "d": { "points": 2, "tiles": 4 }, + "e": { "points": 1, "tiles": 12 }, + "f": { "points": 4, "tiles": 2 }, + "g": { "points": 2, "tiles": 3 }, + "h": { "points": 4, "tiles": 2 }, + "i": { "points": 1, "tiles": 9 }, + "j": { "points": 8, "tiles": 1 }, + "k": { "points": 5, "tiles": 1 }, + "l": { "points": 1, "tiles": 4 }, + "m": { "points": 3, "tiles": 2 }, + "n": { "points": 1, "tiles": 6 }, + "o": { "points": 1, "tiles": 8 }, + "p": { "points": 3, "tiles": 2 }, + "q": { "points": 10, "tiles": 1 }, + "r": { "points": 1, "tiles": 6 }, + "s": { "points": 1, "tiles": 4 }, + "t": { "points": 1, "tiles": 6 }, + "u": { "points": 1, "tiles": 4 }, + "v": { "points": 4, "tiles": 2 }, + "w": { "points": 4, "tiles": 2 }, + "x": { "points": 8, "tiles": 1 }, + "y": { "points": 4, "tiles": 2 }, + "z": { "points": 10, "tiles": 1 }, + "blank": { "tiles": 2 } + } const Scrabble = { score(word) { + if (word === null || !word.match(/^[a-zA-Z]+$/)) { + return null; + } - }, - highestScoreFrom(arrayOfWords) { + word = word.toLowerCase().split(""); + let word_score = 0; + word.forEach (function (letter) { + let points = LETTER_VALUES[letter]["points"]; + word_score += points; + }); + return word_score; }, + + highestScoreFrom(arrayOfWords) { + + } }; Scrabble.Player = class { From 815f962a7e8ce5fb4e47bd7734e977ae40a1b5b3 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Tue, 15 May 2018 15:57:50 -0700 Subject: [PATCH 02/12] error checks and regex validation --- scrabble.js | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0f47201..9579864 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,33 +1,4 @@ -const LETTER_VALUES = - { - "a": { "points": 1, "tiles": 9 }, - "b": { "points": 3, "tiles": 2 }, - "c": { "points": 3, "tiles": 2 }, - "d": { "points": 2, "tiles": 4 }, - "e": { "points": 1, "tiles": 12 }, - "f": { "points": 4, "tiles": 2 }, - "g": { "points": 2, "tiles": 3 }, - "h": { "points": 4, "tiles": 2 }, - "i": { "points": 1, "tiles": 9 }, - "j": { "points": 8, "tiles": 1 }, - "k": { "points": 5, "tiles": 1 }, - "l": { "points": 1, "tiles": 4 }, - "m": { "points": 3, "tiles": 2 }, - "n": { "points": 1, "tiles": 6 }, - "o": { "points": 1, "tiles": 8 }, - "p": { "points": 3, "tiles": 2 }, - "q": { "points": 10, "tiles": 1 }, - "r": { "points": 1, "tiles": 6 }, - "s": { "points": 1, "tiles": 4 }, - "t": { "points": 1, "tiles": 6 }, - "u": { "points": 1, "tiles": 4 }, - "v": { "points": 4, "tiles": 2 }, - "w": { "points": 4, "tiles": 2 }, - "x": { "points": 8, "tiles": 1 }, - "y": { "points": 4, "tiles": 2 }, - "z": { "points": 10, "tiles": 1 }, - "blank": { "tiles": 2 } - } + const Scrabble = { score(word) { @@ -46,7 +17,7 @@ const Scrabble = { }, highestScoreFrom(arrayOfWords) { - + } }; From 75a63f59009e8a92d6c7764d5d0aef79f8564b29 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Tue, 15 May 2018 15:58:37 -0700 Subject: [PATCH 03/12] created a letter_values constant hash --- scrabble.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index 9579864..131b110 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,4 +1,33 @@ - +const LETTER_VALUES = + { + "a": { "points": 1, "tiles": 9 }, + "b": { "points": 3, "tiles": 2 }, + "c": { "points": 3, "tiles": 2 }, + "d": { "points": 2, "tiles": 4 }, + "e": { "points": 1, "tiles": 12 }, + "f": { "points": 4, "tiles": 2 }, + "g": { "points": 2, "tiles": 3 }, + "h": { "points": 4, "tiles": 2 }, + "i": { "points": 1, "tiles": 9 }, + "j": { "points": 8, "tiles": 1 }, + "k": { "points": 5, "tiles": 1 }, + "l": { "points": 1, "tiles": 4 }, + "m": { "points": 3, "tiles": 2 }, + "n": { "points": 1, "tiles": 6 }, + "o": { "points": 1, "tiles": 8 }, + "p": { "points": 3, "tiles": 2 }, + "q": { "points": 10, "tiles": 1 }, + "r": { "points": 1, "tiles": 6 }, + "s": { "points": 1, "tiles": 4 }, + "t": { "points": 1, "tiles": 6 }, + "u": { "points": 1, "tiles": 4 }, + "v": { "points": 4, "tiles": 2 }, + "w": { "points": 4, "tiles": 2 }, + "x": { "points": 8, "tiles": 1 }, + "y": { "points": 4, "tiles": 2 }, + "z": { "points": 10, "tiles": 1 }, + "blank": { "tiles": 2 } + } const Scrabble = { score(word) { From cf34348ec7206fd44fa3f4427c2f008bbedd998a Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Wed, 16 May 2018 00:08:40 -0700 Subject: [PATCH 04/12] throw exceptions for score --- scrabble.js | 100 ++++++++++++++++++++++++++--------------- specs/scrabble.spec.js | 18 ++++---- 2 files changed, 74 insertions(+), 44 deletions(-) diff --git a/scrabble.js b/scrabble.js index 131b110..f081358 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,54 +1,84 @@ const LETTER_VALUES = - { - "a": { "points": 1, "tiles": 9 }, - "b": { "points": 3, "tiles": 2 }, - "c": { "points": 3, "tiles": 2 }, - "d": { "points": 2, "tiles": 4 }, - "e": { "points": 1, "tiles": 12 }, - "f": { "points": 4, "tiles": 2 }, - "g": { "points": 2, "tiles": 3 }, - "h": { "points": 4, "tiles": 2 }, - "i": { "points": 1, "tiles": 9 }, - "j": { "points": 8, "tiles": 1 }, - "k": { "points": 5, "tiles": 1 }, - "l": { "points": 1, "tiles": 4 }, - "m": { "points": 3, "tiles": 2 }, - "n": { "points": 1, "tiles": 6 }, - "o": { "points": 1, "tiles": 8 }, - "p": { "points": 3, "tiles": 2 }, - "q": { "points": 10, "tiles": 1 }, - "r": { "points": 1, "tiles": 6 }, - "s": { "points": 1, "tiles": 4 }, - "t": { "points": 1, "tiles": 6 }, - "u": { "points": 1, "tiles": 4 }, - "v": { "points": 4, "tiles": 2 }, - "w": { "points": 4, "tiles": 2 }, - "x": { "points": 8, "tiles": 1 }, - "y": { "points": 4, "tiles": 2 }, - "z": { "points": 10, "tiles": 1 }, - "blank": { "tiles": 2 } - } +{ + "a": { "points": 1, "tiles": 9 }, + "b": { "points": 3, "tiles": 2 }, + "c": { "points": 3, "tiles": 2 }, + "d": { "points": 2, "tiles": 4 }, + "e": { "points": 1, "tiles": 12 }, + "f": { "points": 4, "tiles": 2 }, + "g": { "points": 2, "tiles": 3 }, + "h": { "points": 4, "tiles": 2 }, + "i": { "points": 1, "tiles": 9 }, + "j": { "points": 8, "tiles": 1 }, + "k": { "points": 5, "tiles": 1 }, + "l": { "points": 1, "tiles": 4 }, + "m": { "points": 3, "tiles": 2 }, + "n": { "points": 1, "tiles": 6 }, + "o": { "points": 1, "tiles": 8 }, + "p": { "points": 3, "tiles": 2 }, + "q": { "points": 10, "tiles": 1 }, + "r": { "points": 1, "tiles": 6 }, + "s": { "points": 1, "tiles": 4 }, + "t": { "points": 1, "tiles": 6 }, + "u": { "points": 1, "tiles": 4 }, + "v": { "points": 4, "tiles": 2 }, + "w": { "points": 4, "tiles": 2 }, + "x": { "points": 8, "tiles": 1 }, + "y": { "points": 4, "tiles": 2 }, + "z": { "points": 10, "tiles": 1 }, + "blank": { "tiles": 2 } +} +const MAX_LENGTH = 7; const Scrabble = { score(word) { - if (word === null || !word.match(/^[a-zA-Z]+$/)) { - return null; + + if (!word.match(/^[a-zA-Z]+$/)) { + throw "Not a valid word"; + } else if (word.length === 0) { + throw "No word given"; + } else if (word.length > MAX_LENGTH) { + throw "Scrabble words can only be 7 letters!"; } word = word.toLowerCase().split(""); - let word_score = 0; + let wordScore = 0; word.forEach (function (letter) { let points = LETTER_VALUES[letter]["points"]; - word_score += points; + wordScore += points; }); - return word_score; + + if (word.length === 7) { + wordScore += 50; + } + + return wordScore; }, highestScoreFrom(arrayOfWords) { + if (arrayOfWords.length === 0) { + throw "No words given"; + } + let topWord = arrayOfWords[0]; + let topScore = score(topWord); + + arrayOfWords.forEach (function (word) { + let score = score(word); + if (score > topScore) { + topScore = score; + topWord = word; + } else if (score === topScore) { + if (word.length === 7 && topWord.length !==7 || word.length < topWord.length) { + topScore = score; + topWord = word; + } + } + }); + return topWord; } -}; +} Scrabble.Player = class { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b562037..1db8fd3 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -11,46 +11,46 @@ 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); }); - test.skip('throws on bad characters', () => { + test('throws on bad characters', () => { expect(() => { Scrabble.score('char^'); }).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); }); - 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(); }); }); 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 6f770b4792a49388879822c9741ff899733e7b5d Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Wed, 16 May 2018 00:09:33 -0700 Subject: [PATCH 05/12] got down the basic logic for highestScoreFrom function --- scrabble.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index f081358..f13cb6c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -57,10 +57,7 @@ const Scrabble = { }, highestScoreFrom(arrayOfWords) { - - if (arrayOfWords.length === 0) { - throw "No words given"; - } + let topWord = arrayOfWords[0]; let topScore = score(topWord); From 627d4e11b9bf09e78318576cebeed989870fb582 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Wed, 16 May 2018 00:09:58 -0700 Subject: [PATCH 06/12] throws errors if empty array --- scrabble.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index f13cb6c..3b9baf6 100644 --- a/scrabble.js +++ b/scrabble.js @@ -57,7 +57,11 @@ const Scrabble = { }, highestScoreFrom(arrayOfWords) { - + + if (arrayOfWords.length === 0) { + throw "No words given"; + } + let topWord = arrayOfWords[0]; let topScore = score(topWord); From 5540d22c500f5be992895e314f22ae35532f2194 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Wed, 16 May 2018 13:59:20 -0700 Subject: [PATCH 07/12] fixed score(word) to Scrabble.score(word) --- scrabble.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 3b9baf6..11e0180 100644 --- a/scrabble.js +++ b/scrabble.js @@ -61,12 +61,12 @@ const Scrabble = { if (arrayOfWords.length === 0) { throw "No words given"; } - + let topWord = arrayOfWords[0]; - let topScore = score(topWord); + let topScore = Scrabble.score(topWord); arrayOfWords.forEach (function (word) { - let score = score(word); + let score = Scrabble.score(word); if (score > topScore) { topScore = score; topWord = word; From 41f2ee556c50abf74f7d3415a4edb58a16b2d5e2 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Wed, 16 May 2018 16:47:13 -0700 Subject: [PATCH 08/12] fixed play in Player --- scrabble.js | 64 +++++++++++++++++++++++++++++++++++------- specs/scrabble.spec.js | 36 ++++++++++++------------ 2 files changed, 72 insertions(+), 28 deletions(-) diff --git a/scrabble.js b/scrabble.js index 11e0180..a0dc96c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -63,19 +63,13 @@ const Scrabble = { } let topWord = arrayOfWords[0]; - let topScore = Scrabble.score(topWord); arrayOfWords.forEach (function (word) { - let score = Scrabble.score(word); - if (score > topScore) { - topScore = score; + if ( Scrabble.score(word) > Scrabble.score(topWord)) { topWord = word; - } else if (score === topScore) { - if (word.length === 7 && topWord.length !==7 || word.length < topWord.length) { - topScore = score; - topWord = word; - } - } + } else if ( Scrabble.score(word) == Scrabble.score(topWord)) + + }); return topWord; } @@ -83,6 +77,56 @@ const Scrabble = { Scrabble.Player = class { + constructor(name) { + if (name == null) { + throw("The player needs a name!"); + } + this.name = name; + this.plays = []; + } + + play(word) { + if ( this.hasWon ) { + return false; + } else { + this.plays.push(word); + } + } + + totalScore() { + let score = 0; + this.plays.forEach (function (word){ + let wordScore = Scrabble.score(word); + score += wordScore; + }); + return score; + } + + hasWon() { + let score = this.totalScore(); + if (score > 100) { + return true; + } else { + return false; + } + } + + highestScoringWord() { + let max_word = ""; + this.plays.forEach (function(word) { + if ( Scrabble.score(word) > max_word ) { + max_word = word; + } + }); + return max_word; + } + + highestWordScore() { + let word = this.highestScoringWord(); + let wordScore = Scrabble.score(word); + + return wordScore; + } }; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 1db8fd3..bb8a320 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); @@ -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,17 +181,17 @@ describe('Player', () => { }); describe('hasWon', () => { - test.skip('returns false when score < 100', () => { + test('returns false when score < 100', () => { }); - test.skip('returns true when score == 100', () => { + test('returns true when score == 100', () => { }); - test.skip('returns true when score > 100', () => { + test('returns true when score > 100', () => { }); @@ -200,24 +200,24 @@ describe('Player', () => { 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', () => { }); - 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 5afdb3e0fcf591bd4888b9e4726dad3e35f77805 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Thu, 17 May 2018 23:27:49 -0700 Subject: [PATCH 09/12] created a break_tie helper method to handle some of the highest score logic --- scrabble.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index a0dc96c..f68a640 100644 --- a/scrabble.js +++ b/scrabble.js @@ -31,8 +31,8 @@ const LETTER_VALUES = const MAX_LENGTH = 7; const Scrabble = { - score(word) { + isRealWord(word) { if (!word.match(/^[a-zA-Z]+$/)) { throw "Not a valid word"; } else if (word.length === 0) { @@ -40,6 +40,11 @@ const Scrabble = { } else if (word.length > MAX_LENGTH) { throw "Scrabble words can only be 7 letters!"; } + }, + + score(word) { + + Scrabble.isRealWord(word); word = word.toLowerCase().split(""); let wordScore = 0; @@ -55,6 +60,17 @@ const Scrabble = { return wordScore; }, + breakTie(topWord, word) { + if ( topWord.length === 7 ) { + return topWord; + } else if (word.length === 7) { + return word; + } else if (word.length < topWord.length) { + return word; + } else { + return topWord; + } + }, highestScoreFrom(arrayOfWords) { @@ -67,9 +83,9 @@ const Scrabble = { arrayOfWords.forEach (function (word) { if ( Scrabble.score(word) > Scrabble.score(topWord)) { topWord = word; - } else if ( Scrabble.score(word) == Scrabble.score(topWord)) - - + } else if ( Scrabble.score(word) == Scrabble.score(topWord)) { + topWord = Scrabble.breakTie(topWord, word); + } }); return topWord; } @@ -86,11 +102,14 @@ Scrabble.Player = class { } play(word) { - if ( this.hasWon ) { + Scrabble.isRealWord(word); + + if (this.hasWon()) { return false; } else { this.plays.push(word); } + return true; } totalScore() { From ae50b52a166c1241183f0b4c4eb6c34c898ac956 Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Thu, 17 May 2018 23:55:02 -0700 Subject: [PATCH 10/12] wrote hasWon tests --- scrabble.js | 4 ++-- specs/scrabble.spec.js | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index f68a640..8baf08d 100644 --- a/scrabble.js +++ b/scrabble.js @@ -103,7 +103,7 @@ Scrabble.Player = class { play(word) { Scrabble.isRealWord(word); - + if (this.hasWon()) { return false; } else { @@ -123,7 +123,7 @@ Scrabble.Player = class { hasWon() { let score = this.totalScore(); - if (score > 100) { + if (score >= 100) { return true; } else { return false; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index bb8a320..39b3122 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -182,17 +182,27 @@ describe('Player', () => { describe('hasWon', () => { test('returns false when score < 100', () => { + const player = new Scrabble.Player('test player'); + player.play("hello"); - + expect(player.hasWon()).toBeFalsy(); }); test('returns true when score == 100', () => { + const player = new Scrabble.Player('test player'); + player.play("zzzzz"); + player.play("zzzzz"); + expect(player.hasWon()).toBeTruthy(); }); test('returns true when score > 100', () => { + const player = new Scrabble.Player('test player'); + player.play("zzzzzs"); + player.play("zzzzzx"); + expect(player.hasWon()).toBeTruthy(); }); }); From 2c6f6184cba821518dfd460330f1a7430f05f5cb Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Fri, 18 May 2018 00:19:19 -0700 Subject: [PATCH 11/12] wrote higest Scoring Word function, tweaked the code as well --- scrabble.js | 8 ++++++-- specs/scrabble.spec.js | 11 ++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8baf08d..e53dc89 100644 --- a/scrabble.js +++ b/scrabble.js @@ -131,9 +131,13 @@ Scrabble.Player = class { } highestScoringWord() { - let max_word = ""; + if (this.plays.length === 0) { + throw "No words played"; + } + + let max_word = this.plays[0]; this.plays.forEach (function(word) { - if ( Scrabble.score(word) > max_word ) { + if ( Scrabble.score(word) > Scrabble.score(max_word) ) { max_word = word; } }); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 39b3122..2e90453 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -211,13 +211,22 @@ describe('Player', () => { // Tie-breaking logic is already described in the tests // for highestWordFrom, so we will not repeat it here. test('returns the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + let words = ["whales", "hello", "zzzzzj"]; + words.forEach (function(word) { + player.play(word); + }); + expect(player.highestScoringWord()).toEqual("zzzzzj"); }); test('throws an error if no words have been played', () => { + const player = new Scrabble.Player('test player'); + expect(player.plays.length).toEqual(0); - + expect(() => { player.highestScoringWord(); + }).toThrow(); }); }); From a3588b9cd7fa697167a4df18f8cb0d2614c78bcb Mon Sep 17 00:00:00 2001 From: knockknockhusthere Date: Fri, 18 May 2018 08:58:40 -0700 Subject: [PATCH 12/12] highestWordScore tests --- specs/scrabble.spec.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 2e90453..39a955f 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -232,12 +232,21 @@ describe('Player', () => { describe('highestWordScore', () => { test('returns the score of the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + let words = ["whales", "hello", "zzzzzj"]; + words.forEach (function(word) { + player.play(word); + }); + + expect(player.highestWordScore()).toEqual(58); }); test('throws an error if no words have been played', () => { - + const player = new Scrabble.Player('test player'); + expect(() => { player.highestWordScore(); + }).toThrow(); }); });