From 6bc78459075bbc40c9f3c8e132a314fac8d66d0c Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 15 May 2018 16:04:46 -0700 Subject: [PATCH 1/9] Created data structure for letter scores. Added to score method and one test is passing. --- scrabble.js | 40 ++++++++++++++++++++++++++++++++++++++++ specs/scrabble.spec.js | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index e95f352..1d434b4 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,46 @@ +const SCORECHART = { + 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, +} const Scrabble = { + // score returns the total score value for the given word. + // word input is a string (case sensitive) score(word) { + let wordUp = word.toUpperCase() + let wordSplits = wordUp.split(''); + let totalScore = 0; + wordSplits.forEach(function(letter) { + totalScore += SCORECHART[letter] + }); + return totalScore }, + highestScoreFrom(arrayOfWords) { }, @@ -14,3 +52,5 @@ Scrabble.Player = class { module.exports = Scrabble; + +Scrabble.score('dog'); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b562037..f669459 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -1,4 +1,4 @@ -const Scrabble = require('../scrabble'); + const Scrabble = require('../scrabble'); describe('score', () => { test('is defined', () => { From b903a9d98de265ee9fc06ab1a17853e7cd4fbddd Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 15 May 2018 19:16:57 -0700 Subject: [PATCH 2/9] Completed score function for wave 1. all tests are passing. --- scrabble.js | 23 ++++++++++++++++++----- specs/scrabble.spec.js | 10 +++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1d434b4..e70def5 100644 --- a/scrabble.js +++ b/scrabble.js @@ -27,15 +27,30 @@ const SCORECHART = { Z: 10, } +const REGEX = /[A-Z]/ + const Scrabble = { - // score returns the total score value for the given word. - // word input is a string (case sensitive) score(word) { let wordUp = word.toUpperCase() let wordSplits = wordUp.split(''); + let totalScore = 0; + if (wordSplits.length > 7 || wordSplits.length === 0) { + throw 'Invalid word'; + } + + if (wordSplits.length === 7) { + totalScore += 50; + } + wordSplits.forEach(function(letter) { - totalScore += SCORECHART[letter] + + if (REGEX.test(letter)) { + totalScore += SCORECHART[letter]; + } else { + throw 'Not a valid word'; + } + }); return totalScore @@ -52,5 +67,3 @@ Scrabble.Player = class { module.exports = Scrabble; - -Scrabble.score('dog'); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index f669459..1f75be1 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -11,27 +11,27 @@ 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(); }); }); From 78905df29eddc76aefa01636966225d34af182a6 Mon Sep 17 00:00:00 2001 From: Kiera Date: Tue, 15 May 2018 21:19:57 -0700 Subject: [PATCH 3/9] made progress on highestscore from method. --- scrabble.js | 26 +++++++++++++++++++++++++- specs/scrabble.spec.js | 8 ++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index e70def5..63a2f36 100644 --- a/scrabble.js +++ b/scrabble.js @@ -57,8 +57,32 @@ const Scrabble = { }, highestScoreFrom(arrayOfWords) { + if (arrayOfWords.length === 0) { + throw 'Unable to calculate highest score without any words.'; + } + if (Array.isArray(arrayOfWords) !== true) { + throw 'Unable to calculate highest score without an array of words'; + } + if (arrayOfWords.length === 1){ + return arrayOfWords[0]; + } else { + let word_score = {}; + arrayOfWords.forEach(function(word) { + word_score[word] = Scrabble.score(word); + }); + + let highest_score = 0; + for(const word in word_score) { + if (word_score[word] > highest_score) { + word_score[word] = highest_score; + } + } + return `${word} and score of ${word_score[word]}`; + + } + } + - }, }; Scrabble.Player = class { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 1f75be1..cf78d09 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 ec19c38655340110f9de97dbb4388cb08b2a2215 Mon Sep 17 00:00:00 2001 From: Kiera Date: Wed, 16 May 2018 14:58:46 -0700 Subject: [PATCH 4/9] completed highestScoreFrom function. all 7 tests are passing. --- scrabble.js | 26 +++++++++++++++++++++----- specs/scrabble.spec.js | 6 +++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/scrabble.js b/scrabble.js index 63a2f36..7217e5c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -44,7 +44,6 @@ const Scrabble = { } wordSplits.forEach(function(letter) { - if (REGEX.test(letter)) { totalScore += SCORECHART[letter]; } else { @@ -72,13 +71,22 @@ const Scrabble = { }); let highest_score = 0; + let winWord = null; for(const word in word_score) { if (word_score[word] > highest_score) { - word_score[word] = highest_score; + highest_score = word_score[word]; + winWord = word; + } if (word_score[word] === highest_score && winWord.length === 7) { + return winWord; + } if (word_score[word] === highest_score && word.length === 7) { + return word; + } if (word_score[word] === highest_score && winWord.length < word.length) { + return winWord; + } if (word_score[word] === highest_score && winWord.length > word.length) { + return word; } - } - return `${word} and score of ${word_score[word]}`; - + } //for loop + return winWord; } } @@ -91,3 +99,11 @@ Scrabble.Player = class { module.exports = Scrabble; + +// else if (word_score[word] === highest_score && winWord.length === 7 && word.length !== 7) { +// return winWord; +// } else if (word_score[word] === highest_score && winWord.length > word.length) { +// return word; +// } else { +// return winWord; +// } diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index cf78d09..49b8f71 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 3be879e324b90701263184add795adda622d86b5 Mon Sep 17 00:00:00 2001 From: Kiera Date: Wed, 16 May 2018 16:31:18 -0700 Subject: [PATCH 5/9] created play function. passing 2 of 3 tests. making progress on total score function. --- scrabble.js | 41 +++++++++++++++++++++++++++++++---------- specs/scrabble.spec.js | 10 +++++----- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7217e5c..5f018df 100644 --- a/scrabble.js +++ b/scrabble.js @@ -88,22 +88,43 @@ const Scrabble = { } //for loop return winWord; } + }, +}; + +Scrabble.Player = class { + constructor(name) { + this.name = name; + this.plays = []; + if (name === undefined) { + throw 'Player must have a name'; + } } + play(word) { + if (word === undefined) { + throw 'Must play a word'; + } + if (REGEX.test(word.toUpperCase)!== true){ + throw 'Must play a real word'; + } + this.plays.push(word); + return this.plays; + } -}; + totalScore () { + let points = 0; + this.plays.forEach(function(word) { + let wScore = Scrabble.score(word); + points += wScore; + }); + return points; + } -Scrabble.Player = class { + hasWon(){ + + } }; module.exports = Scrabble; - -// else if (word_score[word] === highest_score && winWord.length === 7 && word.length !== 7) { -// return winWord; -// } else if (word_score[word] === highest_score && winWord.length > word.length) { -// return word; -// } else { -// return winWord; -// } diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 49b8f71..b190d11 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); From ec259e5972aee7fede74c0dd61259d38d4e0c171 Mon Sep 17 00:00:00 2001 From: Kiera Date: Wed, 16 May 2018 16:39:19 -0700 Subject: [PATCH 6/9] completed totalScore function all tests are passing. --- specs/scrabble.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b190d11..32e6ff6 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -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}]; From 3cb4f1848a4777bad9789124f3bf447fb1c2e2ec Mon Sep 17 00:00:00 2001 From: Kiera Date: Thu, 17 May 2018 21:37:34 -0700 Subject: [PATCH 7/9] completed hasWon function, filled in test specs and they're all passing. --- scrabble.js | 25 +++++++++++++++++-------- specs/scrabble.spec.js | 22 +++++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index 5f018df..5244771 100644 --- a/scrabble.js +++ b/scrabble.js @@ -101,17 +101,22 @@ Scrabble.Player = class { } play(word) { - if (word === undefined) { - throw 'Must play a word'; - } - if (REGEX.test(word.toUpperCase)!== true){ - throw 'Must play a real word'; + if (this.hasWon()){ + return false; + } else { + if (word === undefined) { + throw 'Must play a word'; + } + if (REGEX.test(word.toUpperCase)!== true){ + throw 'Must play a real word'; + } + this.plays.push(word); } - this.plays.push(word); return this.plays; + } - totalScore () { + totalScore() { let points = 0; this.plays.forEach(function(word) { let wScore = Scrabble.score(word); @@ -121,7 +126,11 @@ Scrabble.Player = class { } hasWon(){ - + if (this.totalScore() >= 100) { + return true; + } else { + return false; + } } }; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 32e6ff6..6442e47 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -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 @@ -181,18 +181,30 @@ 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.play('zzzzz') + player.totalScore(); + 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.play('zzzzz') + player.play('zzzzz') + player.totalScore(); + 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.play('zzzzzzz') + player.totalScore(); + expect(player.hasWon()).toBe(true); }); }); From a9f85337ed6e824aaa5e92e300fd890e822e3401 Mon Sep 17 00:00:00 2001 From: Kiera Date: Thu, 17 May 2018 22:17:32 -0700 Subject: [PATCH 8/9] completed highestScoringWord function. created tests for HSW and they're passing. --- scrabble.js | 9 +++++++++ specs/scrabble.spec.js | 16 ++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index 5244771..1973784 100644 --- a/scrabble.js +++ b/scrabble.js @@ -133,6 +133,15 @@ Scrabble.Player = class { } } + highestScoringWord() { + if (this.plays.length === 0) { + throw 'Error'; + } + + return Scrabble.highestScoreFrom(this.plays); + + } + }; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 6442e47..df8dc0d 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -201,7 +201,7 @@ describe('Player', () => { test('returns true when score > 100', () => { const player = new Scrabble.Player('test player'); - player.play('zzzzzzz') + player.play('zzzzzzz'); player.totalScore(); expect(player.hasWon()).toBe(true); @@ -212,14 +212,18 @@ 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', () => { + const player = new Scrabble.Player('test player'); + player.play('zzz'); + player.play('dog'); + expect(player.highestScoringWord()).toBe('zzz'); + expect(player.plays.length).toBe(2); }); - test.skip('throws an error if no words have been played', () => { - - + test('throws an error if no words have been played', () => { + const player = new Scrabble.Player('test player'); + expect(() => { player.highestScoringWord(); }).toThrow(); }); }); From db74c6de9ff53e8b2d0d8d22c8bbc37808c5c061 Mon Sep 17 00:00:00 2001 From: Kiera Date: Thu, 17 May 2018 22:40:34 -0700 Subject: [PATCH 9/9] Added highestwordscore function and two passing functionality tests. --- scrabble.js | 10 ++++++++-- specs/scrabble.spec.js | 12 ++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1973784..84529f2 100644 --- a/scrabble.js +++ b/scrabble.js @@ -135,10 +135,16 @@ Scrabble.Player = class { highestScoringWord() { if (this.plays.length === 0) { - throw 'Error'; + throw 'Cannot calculate highest scoring word without a played word'; } - return Scrabble.highestScoreFrom(this.plays); + } + + highestWordScore(){ + if (this.plays.length === 0) { + throw 'Cannot calculate highest word score without a played word'; + } + return Scrabble.score(this.highestScoringWord()); } diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index df8dc0d..ddea61b 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -228,13 +228,17 @@ describe('Player', () => { }); describe('highestWordScore', () => { - test.skip('returns the score of the highest scoring word played', () => { - + test('returns the score of the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + player.play('zzz'); + player.play('dog'); + expect(player.highestWordScore()).toBe(30); }); - test.skip('throws an error if no words have been played', () => { - + test('throws an error if no words have been played', () => { + const player = new Scrabble.Player('test player'); + expect(() => { player.highestWordScore(); }).toThrow(); }); });