From da6aa39bf37c8bbb378ad520f5799b7376508d16 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 00:21:21 -0800 Subject: [PATCH 01/35] Tests passed to calculate word score --- package-lock.json | 123 ++++++++++++ scrabble.js | 48 +++++ spec/scrabble_spec.js | 428 +++++++++++++++++++++--------------------- 3 files changed, 385 insertions(+), 214 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..80043d7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,123 @@ +{ + "name": "js-scrabble", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "add-matchers": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/add-matchers/-/add-matchers-0.5.0.tgz", + "integrity": "sha1-UCGQ5HUM1XIWGDkyaLYaFXNm52U=" + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", + "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "requires": { + "balanced-match": "1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "eslint-plugin-jasmine": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-jasmine/-/eslint-plugin-jasmine-2.9.1.tgz", + "integrity": "sha1-IuGaWfFvOl9kOgSroEQ40OMEcDA=" + }, + "exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=" + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "jasmine": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-2.8.0.tgz", + "integrity": "sha1-awicChFXax8W3xG4AUbZHU6Lij4=", + "requires": { + "exit": "0.1.2", + "glob": "7.1.2", + "jasmine-core": "2.8.0" + } + }, + "jasmine-core": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-2.8.0.tgz", + "integrity": "sha1-vMl5rh+f0FcB5F5S5l06XWPxok4=" + }, + "jasmine-expect": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/jasmine-expect/-/jasmine-expect-3.8.1.tgz", + "integrity": "sha512-klARdR5AVX9nZhHhYDlbDYgxgi6kl9DGS0vguhaioKoSwr8HL1uOQ7FFUBASq/sqBL/s2nkh/1efqZ2ugcknFA==", + "requires": { + "add-matchers": "0.5.0" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "1.1.8" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + } + } +} diff --git a/scrabble.js b/scrabble.js index 7a1f161..0a575fb 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,56 @@ +let tiles = { + 'A': 1, 'E': 1, 'I': 1, 'O': 1, 'L': 1, 'N': 1, 'R': 1, + 'U': 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 +}; + +// def self.score(word) +// - @score = 0 +// + value = 0 +// word.upcase.each_char do |letter| +// - @score = TILES[letter] + @score +// + value += TILES[letter] +// end +// if word.length == 7 +// - bonus = @score + 50 +// - return bonus +// - end +// - return @score +// - end +// + value += 50 +// + end +// + return value +// + end + + + + const Scrabble = { score: function(word) { // TODO: implement score + // word.toUpperCase().split(' ').forEach(function(letter) { + // console.log('this is the ' + word); + let total = 0; + // let word; + + word.toUpperCase().split('').forEach(function(letter) { + // console.log(letter); + total = tiles[letter] + total; + // console.log(tiles[letter]); + // console.log(total); + + }); + return total; } + + + // var letter, i, sum = 0; + // for (i = 0; i < word.length; i++) { + // letter = word[i]; + // sum += alphabet[letter]; + // } // TODO: add the highestScoreFrom method }; diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..998b205 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -11,218 +11,218 @@ describe('score', function() { expect(Scrabble.score('pig')).toBe(6); }); - it ('adds 50 points for a 7-letter word', function() { - expect(Scrabble.score('academy')).toBe(65); - }); - - it ('throws on bad characters', function() { - expect(function () { - Scrabble.score('char^'); - }).toThrow(); - }); - - it ('handles all upper- and lower-case letters', function() { - expect(Scrabble.score('dog')).toBe(5); - expect(Scrabble.score('DOG')).toBe(5); - expect(Scrabble.score('DoG')).toBe(5); - }); - - it ('does not allow words > 7 letters', function() { - expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); - }); - - it ('does not allow empty words', function() { - expect(function() { Scrabble.score(''); }).toThrow(); - }); -}); - -describe('highestScoreFrom', function() { - it ('is defined', function() { - expect(Scrabble.highestScoreFrom).toBeDefined(); - }); - - it ('throws if no words were passed', function() { - expect(function() { Scrabble.highestScoreFrom([]); }).toThrow(); - expect(function() { Scrabble.highestScoreFrom('not array'); }).toThrow(); - }); - - it ('returns the only word in a length-1 array', function() { - expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); - }); - - it ('returns the highest word if there are two words', function() { - // Check score assumptions - expect(Scrabble.score('dog')).toBe(5); - expect(Scrabble.score('pig')).toBe(6); - - // Test the functionality - expect(Scrabble.highestScoreFrom(['dog', 'pig'])).toBe('pig'); - expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); - }); - - it ('if tied, prefer a word with 7 letters', function() { - const loser = 'zzzzzz'; - const winner = 'iiiiddd'; - - // Check score assumptions - expect(Scrabble.score(loser)).toBe(60); - expect(Scrabble.score(winner)).toBe(60); - - // Test functionality - expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); - expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); - }); - - it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { - // Check score assumptions - expect(Scrabble.score('dog')).toBe(5); - expect(Scrabble.score('goat')).toBe(5); - - // Test functionality - expect(Scrabble.highestScoreFrom(['dog', 'goat'])).toBe('dog'); - expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); - }); - - it ('returns the first word of a tie with same letter count', function() { - // Check score assumptions - expect(Scrabble.score('i')).toBe(1); - expect(Scrabble.score('dog')).toBe(5); - expect(Scrabble.score('cat')).toBe(5); - - // Test the functionality - expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); - expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); - expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); - expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); - }); -}); - -describe('Player', function() { - it ('is defined', function() { - expect(Scrabble.Player).toBeDefined(); - }); - - describe('Constructor', function() { - it('Creates a new player', function() { - let name = 'test name'; - let player = new Scrabble.Player(name); - expect(player.name).toBe(name); - }); - - it('Requires a name', function() { - expect(function() { new Scrabble.Player(); }).toThrow(); - }); - }); - - describe ('play', function() { - it ('Records the played word', function() { - let word = 'dog'; - let player = new Scrabble.Player('test player'); - expect(player.plays.length).toBe(0); - - expect(player.play(word)).toBeTruthy(); - - expect(player.plays.length).toBe(1); - expect(player.plays[0]).toBe(word); - }); - - it ('Requires a real word', function() { - let player = new Scrabble.Player('test player'); - expect(player.plays.length).toBe(0); - - expect(function() { player.play(); }).toThrow(); - expect(player.plays.length).toBe(0); - - expect(function() { player.play(44); }).toThrow(); - expect(player.plays.length).toBe(0); - }); - - it ('Returns false and does not update plays if the player has already won', function() { - let player = new Scrabble.Player('test player'); - expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts - expect(player.plays.length).toBe(1); - expect(player.hasWon()).toBeTruthy(); - - expect(player.play('dog')).toBe(false); - expect(player.plays.length).toBe(1); - }); - }); - - describe ('totalScore', function() { - it ('Is zero if the player has not played anything', function() { - let player = new Scrabble.Player('test player'); - expect(player.totalScore()).toBe(0); - }); - - it ('Is updated by play', function() { - let player = new Scrabble.Player('test player'); - expect(player.totalScore()).toBe(0); - player.play('dog'); - expect(player.totalScore()).toBe(5); - player.play('cat'); - expect(player.totalScore()).toBe(10); - player.play('goat'); - expect(player.totalScore()).toBe(15); - }); - }); - - describe('hasWon', function() { - it('returns false when score < 100', function() { - let player = new Scrabble.Player('test player'); - expect(player.totalScore()).toBe(0); - expect(player.hasWon()).toBe(false); - - player.play('zzzzz'); // Score += 50 - expect(player.totalScore()).toBe(50); - expect(player.hasWon()).toBe(false); - - player.play('zzzzji'); // Score += 49 - expect(player.totalScore()).toBe(99); - expect(player.hasWon()).toBe(false); - }); - - it('returns true when score == 100', function() { - let player = new Scrabble.Player('test player'); - player.play('zzzzbbf'); // Score += 100 - expect(player.totalScore()).toBe(100); - expect(player.hasWon()).toBe(true); - }); - - it('returns true when score > 100', function() { - let player = new Scrabble.Player('test player'); - player.play('zzzzzzz'); // Score += 120 - expect(player.totalScore()).toBe(120); - expect(player.hasWon()).toBe(true); - }); - }); - - describe('highestScoringWord', function() { - // Tie-breaking logic is already described in the tests - // for highestWordFrom, so we will not repeat it here. - it('returns the highest scoring word played', function() { - let player = new Scrabble.Player('test player'); - player.play('cat'); - player.play('zzzz'); - expect(player.highestScoringWord()).toBe('zzzz'); - }); - - it('throws an error if no words have been played', function() { - let player = new Scrabble.Player('test player'); - expect(() => { player.highestScoringWord() }).toThrow(); - }); - }); - - describe('highestWordScore', function() { - it('returns the score of the highest scoring word played', function() { - let player = new Scrabble.Player('test player'); - player.play('cat'); - player.play('zzzz'); - expect(player.highestWordScore()).toBe(40); - }); - - it('throws an error if no words have been played', function() { - let player = new Scrabble.Player('test player'); - expect(() => { player.highestWordScore() }).toThrow(); - }); - }); + // it ('adds 50 points for a 7-letter word', function() { + // expect(Scrabble.score('academy')).toBe(65); + // }); + + // it ('throws on bad characters', function() { + // expect(function () { + // Scrabble.score('char^'); + // }).toThrow(); + // }); + + // it ('handles all upper- and lower-case letters', function() { + // expect(Scrabble.score('dog')).toBe(5); + // expect(Scrabble.score('DOG')).toBe(5); + // expect(Scrabble.score('DoG')).toBe(5); + // }); + +// it ('does not allow words > 7 letters', function() { +// expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); +// }); +// +// it ('does not allow empty words', function() { +// expect(function() { Scrabble.score(''); }).toThrow(); +// }); +// }); +// +// describe('highestScoreFrom', function() { +// it ('is defined', function() { +// expect(Scrabble.highestScoreFrom).toBeDefined(); +// }); +// +// it ('throws if no words were passed', function() { +// expect(function() { Scrabble.highestScoreFrom([]); }).toThrow(); +// expect(function() { Scrabble.highestScoreFrom('not array'); }).toThrow(); +// }); +// +// it ('returns the only word in a length-1 array', function() { +// expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); +// }); +// +// it ('returns the highest word if there are two words', function() { +// // Check score assumptions +// expect(Scrabble.score('dog')).toBe(5); +// expect(Scrabble.score('pig')).toBe(6); +// +// // Test the functionality +// expect(Scrabble.highestScoreFrom(['dog', 'pig'])).toBe('pig'); +// expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); +// }); +// +// it ('if tied, prefer a word with 7 letters', function() { +// const loser = 'zzzzzz'; +// const winner = 'iiiiddd'; +// +// // Check score assumptions +// expect(Scrabble.score(loser)).toBe(60); +// expect(Scrabble.score(winner)).toBe(60); +// +// // Test functionality +// expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); +// expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); +// }); +// +// it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { +// // Check score assumptions +// expect(Scrabble.score('dog')).toBe(5); +// expect(Scrabble.score('goat')).toBe(5); +// +// // Test functionality +// expect(Scrabble.highestScoreFrom(['dog', 'goat'])).toBe('dog'); +// expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); +// }); +// +// it ('returns the first word of a tie with same letter count', function() { +// // Check score assumptions +// expect(Scrabble.score('i')).toBe(1); +// expect(Scrabble.score('dog')).toBe(5); +// expect(Scrabble.score('cat')).toBe(5); +// +// // Test the functionality +// expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); +// expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); +// expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); +// expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); +// }); +// }); +// +// describe('Player', function() { +// it ('is defined', function() { +// expect(Scrabble.Player).toBeDefined(); +// }); +// +// describe('Constructor', function() { +// it('Creates a new player', function() { +// let name = 'test name'; +// let player = new Scrabble.Player(name); +// expect(player.name).toBe(name); +// }); +// +// it('Requires a name', function() { +// expect(function() { new Scrabble.Player(); }).toThrow(); +// }); +// }); +// +// describe ('play', function() { +// it ('Records the played word', function() { +// let word = 'dog'; +// let player = new Scrabble.Player('test player'); +// expect(player.plays.length).toBe(0); +// +// expect(player.play(word)).toBeTruthy(); +// +// expect(player.plays.length).toBe(1); +// expect(player.plays[0]).toBe(word); +// }); +// +// it ('Requires a real word', function() { +// let player = new Scrabble.Player('test player'); +// expect(player.plays.length).toBe(0); +// +// expect(function() { player.play(); }).toThrow(); +// expect(player.plays.length).toBe(0); +// +// expect(function() { player.play(44); }).toThrow(); +// expect(player.plays.length).toBe(0); +// }); +// +// it ('Returns false and does not update plays if the player has already won', function() { +// let player = new Scrabble.Player('test player'); +// expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts +// expect(player.plays.length).toBe(1); +// expect(player.hasWon()).toBeTruthy(); +// +// expect(player.play('dog')).toBe(false); +// expect(player.plays.length).toBe(1); +// }); +// }); +// +// describe ('totalScore', function() { +// it ('Is zero if the player has not played anything', function() { +// let player = new Scrabble.Player('test player'); +// expect(player.totalScore()).toBe(0); +// }); +// +// it ('Is updated by play', function() { +// let player = new Scrabble.Player('test player'); +// expect(player.totalScore()).toBe(0); +// player.play('dog'); +// expect(player.totalScore()).toBe(5); +// player.play('cat'); +// expect(player.totalScore()).toBe(10); +// player.play('goat'); +// expect(player.totalScore()).toBe(15); +// }); +// }); +// +// describe('hasWon', function() { +// it('returns false when score < 100', function() { +// let player = new Scrabble.Player('test player'); +// expect(player.totalScore()).toBe(0); +// expect(player.hasWon()).toBe(false); +// +// player.play('zzzzz'); // Score += 50 +// expect(player.totalScore()).toBe(50); +// expect(player.hasWon()).toBe(false); +// +// player.play('zzzzji'); // Score += 49 +// expect(player.totalScore()).toBe(99); +// expect(player.hasWon()).toBe(false); +// }); +// +// it('returns true when score == 100', function() { +// let player = new Scrabble.Player('test player'); +// player.play('zzzzbbf'); // Score += 100 +// expect(player.totalScore()).toBe(100); +// expect(player.hasWon()).toBe(true); +// }); +// +// it('returns true when score > 100', function() { +// let player = new Scrabble.Player('test player'); +// player.play('zzzzzzz'); // Score += 120 +// expect(player.totalScore()).toBe(120); +// expect(player.hasWon()).toBe(true); +// }); +// }); +// +// describe('highestScoringWord', function() { +// // Tie-breaking logic is already described in the tests +// // for highestWordFrom, so we will not repeat it here. +// it('returns the highest scoring word played', function() { +// let player = new Scrabble.Player('test player'); +// player.play('cat'); +// player.play('zzzz'); +// expect(player.highestScoringWord()).toBe('zzzz'); +// }); +// +// it('throws an error if no words have been played', function() { +// let player = new Scrabble.Player('test player'); +// expect(() => { player.highestScoringWord() }).toThrow(); +// }); +// }); +// +// describe('highestWordScore', function() { +// it('returns the score of the highest scoring word played', function() { +// let player = new Scrabble.Player('test player'); +// player.play('cat'); +// player.play('zzzz'); +// expect(player.highestWordScore()).toBe(40); +// }); +// +// it('throws an error if no words have been played', function() { +// let player = new Scrabble.Player('test player'); +// expect(() => { player.highestWordScore() }).toThrow(); +// }); +// }); }); From 0f297b8edf15b8887005fa051acaae06eee3ded8 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 00:22:16 -0800 Subject: [PATCH 02/35] Test passed to accept upper or lower case letters --- spec/scrabble_spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 998b205..19285af 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -21,11 +21,11 @@ describe('score', function() { // }).toThrow(); // }); - // it ('handles all upper- and lower-case letters', function() { - // expect(Scrabble.score('dog')).toBe(5); - // expect(Scrabble.score('DOG')).toBe(5); - // expect(Scrabble.score('DoG')).toBe(5); - // }); + it ('handles all upper- and lower-case letters', function() { + expect(Scrabble.score('dog')).toBe(5); + expect(Scrabble.score('DOG')).toBe(5); + expect(Scrabble.score('DoG')).toBe(5); + }); // it ('does not allow words > 7 letters', function() { // expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); From a98bb0e67a2d228b94a3adb31f33193cfbfccdad Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 11:14:54 -0800 Subject: [PATCH 03/35] Added logic for scoring 7 letter word --- scrabble.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0a575fb..091a859 100644 --- a/scrabble.js +++ b/scrabble.js @@ -23,14 +23,13 @@ let tiles = { // + return value // + end - - - const Scrabble = { score: function(word) { // TODO: implement score // word.toUpperCase().split(' ').forEach(function(letter) { // console.log('this is the ' + word); + + let total = 0; // let word; @@ -39,8 +38,10 @@ const Scrabble = { total = tiles[letter] + total; // console.log(tiles[letter]); // console.log(total); - }); + if (word.length == 7 && total > 0) { + total += 50; + } return total; } From ffe3b9dcf4bac22cc0220cbf621b74484d886340 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 11:15:49 -0800 Subject: [PATCH 04/35] Passed test scoring 7 letter word --- spec/scrabble_spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 19285af..95a7453 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -11,9 +11,9 @@ describe('score', function() { expect(Scrabble.score('pig')).toBe(6); }); - // it ('adds 50 points for a 7-letter word', function() { - // expect(Scrabble.score('academy')).toBe(65); - // }); + it ('adds 50 points for a 7-letter word', function() { + expect(Scrabble.score('academy')).toBe(65); + }); // it ('throws on bad characters', function() { // expect(function () { From 4a33fe382455db8dfefac35c813ab683afb2a7b8 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 13:04:55 -0800 Subject: [PATCH 05/35] Added logic to check if letter is null --- scrabble.js | 101 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/scrabble.js b/scrabble.js index 091a859..b20d958 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,27 +1,10 @@ let tiles = { - 'A': 1, 'E': 1, 'I': 1, 'O': 1, 'L': 1, 'N': 1, 'R': 1, - 'U': 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 + 'A': 1, 'E': 1, 'I': 1, 'O': 1, 'L': 1, 'N': 1, 'R': 1, + 'U': 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 }; -// def self.score(word) -// - @score = 0 -// + value = 0 -// word.upcase.each_char do |letter| -// - @score = TILES[letter] + @score -// + value += TILES[letter] -// end -// if word.length == 7 -// - bonus = @score + 50 -// - return bonus -// - end -// - return @score -// - end -// + value += 50 -// + end -// + return value -// + end const Scrabble = { score: function(word) { @@ -31,30 +14,76 @@ const Scrabble = { let total = 0; - // let word; + let playedWord = word.toUpperCase(); - word.toUpperCase().split('').forEach(function(letter) { - // console.log(letter); - total = tiles[letter] + total; - // console.log(tiles[letter]); - // console.log(total); - }); - if (word.length == 7 && total > 0) { + for (i=0; i < playedWord.length; i++) { + letter = playedWord[i]; + + if (letter === null) { + total = 0; + } + + total += tiles[letter]; + } + if (word.length === 7 && total > 0) { total += 50; } return total; + }, + + // Question: Is forEach less efficient that forloop word.toUpperCase().split('').forEach(function(letter) { + // total = tiles[letter] + total; + // }); + // if (word.length == 7 && total > 0) { + // total += 50; + // } + // return total; + // }, + + // TODO: add the highestScoreFrom method + highestScoreFrom: function(arrayOfWords) { + let max = 0; + let highestScoreWord = ''; + + arrayOfWords.forEach(function(wrd) { + if ( Scrabble.score(wrd) > max ) { + max = Scrabble.score(wrd); + highestScoreWord = wrd; + } else if ( Scrabble.score(wrd) == max ) { + highestScoreWord = word; + } + }); + return highestScoreWord; } +}; // end of Scrabble function + + + + +// def self.highest_score_from(array_of_words) +// max = 0 +// highest_score_word = '' +// +// array_of_words.each do |word| +// if self.score(word) > max +// max = self.score(word) +// highest_score_word = word +// elsif (self.score(word) == max) && (word.length < highest_score_word.length) +// highest_score_word = word +// end +// end +// return highest_score_word +// end +// end #end of class + + + + + - // var letter, i, sum = 0; - // for (i = 0; i < word.length; i++) { - // letter = word[i]; - // sum += alphabet[letter]; - // } - // TODO: add the highestScoreFrom method -}; Scrabble.Player = class { // TODO: implement the Player class From 26cee6eb27862bc959105461f6a3bf4b67fb4327 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 13:12:17 -0800 Subject: [PATCH 06/35] Added logic to select word with fewer tiles if there is tie --- scrabble.js | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/scrabble.js b/scrabble.js index b20d958..90c743e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -12,7 +12,6 @@ const Scrabble = { // word.toUpperCase().split(' ').forEach(function(letter) { // console.log('this is the ' + word); - let total = 0; let playedWord = word.toUpperCase(); @@ -24,6 +23,7 @@ const Scrabble = { } total += tiles[letter]; + } if (word.length === 7 && total > 0) { total += 50; @@ -49,7 +49,7 @@ const Scrabble = { if ( Scrabble.score(wrd) > max ) { max = Scrabble.score(wrd); highestScoreWord = wrd; - } else if ( Scrabble.score(wrd) == max ) { + } else if (( Scrabble.score(wrd) === max ) && (wrd.length < highestScoreWord.length)) { highestScoreWord = word; } }); @@ -59,32 +59,6 @@ const Scrabble = { }; // end of Scrabble function - - -// def self.highest_score_from(array_of_words) -// max = 0 -// highest_score_word = '' -// -// array_of_words.each do |word| -// if self.score(word) > max -// max = self.score(word) -// highest_score_word = word -// elsif (self.score(word) == max) && (word.length < highest_score_word.length) -// highest_score_word = word -// end -// end -// return highest_score_word -// end -// end #end of class - - - - - - - - - Scrabble.Player = class { // TODO: implement the Player class }; From c860ee034b553cd3b1dd5494a16244173b6e7872 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 13:14:37 -0800 Subject: [PATCH 07/35] Passing test for returning first word of a tie with same letter count --- spec/scrabble_spec.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 95a7453..5038d2f 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -46,9 +46,9 @@ describe('score', function() { // expect(function() { Scrabble.highestScoreFrom('not array'); }).toThrow(); // }); // -// it ('returns the only word in a length-1 array', function() { -// expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); -// }); + it ('returns the only word in a length-1 array', function() { + expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); + }); // // it ('returns the highest word if there are two words', function() { // // Check score assumptions @@ -83,11 +83,11 @@ describe('score', function() { // expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); // }); // -// it ('returns the first word of a tie with same letter count', function() { -// // Check score assumptions -// expect(Scrabble.score('i')).toBe(1); -// expect(Scrabble.score('dog')).toBe(5); -// expect(Scrabble.score('cat')).toBe(5); + it ('returns the first word of a tie with same letter count', function() { + // Check score assumptions + expect(Scrabble.score('i')).toBe(1); + expect(Scrabble.score('dog')).toBe(5); + expect(Scrabble.score('cat')).toBe(5); // // // Test the functionality // expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); @@ -95,7 +95,7 @@ describe('score', function() { // expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); // expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); // }); -// }); +}); // // describe('Player', function() { // it ('is defined', function() { From 82e9dd48c045dddbd36b335286570eee04fa9ee7 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 13:29:49 -0800 Subject: [PATCH 08/35] Passing tests for highestscore function' --- scrabble.js | 2 +- spec/scrabble_spec.js | 90 +++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/scrabble.js b/scrabble.js index 90c743e..38d9d7f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -50,7 +50,7 @@ const Scrabble = { max = Scrabble.score(wrd); highestScoreWord = wrd; } else if (( Scrabble.score(wrd) === max ) && (wrd.length < highestScoreWord.length)) { - highestScoreWord = word; + highestScoreWord = wrd; } }); return highestScoreWord; diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 5038d2f..7e8cf33 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -36,10 +36,10 @@ describe('score', function() { // }); // }); // -// describe('highestScoreFrom', function() { -// it ('is defined', function() { -// expect(Scrabble.highestScoreFrom).toBeDefined(); -// }); +describe('highestScoreFrom', function() { + it ('is defined', function() { + expect(Scrabble.highestScoreFrom).toBeDefined(); + }); // // it ('throws if no words were passed', function() { // expect(function() { Scrabble.highestScoreFrom([]); }).toThrow(); @@ -49,52 +49,52 @@ describe('score', function() { it ('returns the only word in a length-1 array', function() { expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); }); -// -// it ('returns the highest word if there are two words', function() { -// // Check score assumptions -// expect(Scrabble.score('dog')).toBe(5); -// expect(Scrabble.score('pig')).toBe(6); -// -// // Test the functionality -// expect(Scrabble.highestScoreFrom(['dog', 'pig'])).toBe('pig'); -// expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); -// }); -// -// it ('if tied, prefer a word with 7 letters', function() { -// const loser = 'zzzzzz'; -// const winner = 'iiiiddd'; -// -// // Check score assumptions -// expect(Scrabble.score(loser)).toBe(60); -// expect(Scrabble.score(winner)).toBe(60); -// -// // Test functionality -// expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); -// expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); -// }); -// -// it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { -// // Check score assumptions -// expect(Scrabble.score('dog')).toBe(5); -// expect(Scrabble.score('goat')).toBe(5); -// -// // Test functionality -// expect(Scrabble.highestScoreFrom(['dog', 'goat'])).toBe('dog'); -// expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); -// }); -// + + it ('returns the highest word if there are two words', function() { + // Check score assumptions + expect(Scrabble.score('dog')).toBe(5); + expect(Scrabble.score('pig')).toBe(6); + + // Test the functionality + expect(Scrabble.highestScoreFrom(['dog', 'pig'])).toBe('pig'); + expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); + }); + + // it ('if tied, prefer a word with 7 letters', function() { + // const loser = 'zzzzzz'; + // const winner = 'iiiiddd'; + // + // // Check score assumptions + // expect(Scrabble.score(loser)).toBe(60); + // expect(Scrabble.score(winner)).toBe(60); + // + // // Test functionality + // expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); + // expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); + // }); + + it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { + // Check score assumptions + expect(Scrabble.score('dog')).toBe(5); + expect(Scrabble.score('goat')).toBe(5); + + // Test functionality + expect(Scrabble.highestScoreFrom(['dog', 'goat'])).toBe('dog'); + expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); + }); + it ('returns the first word of a tie with same letter count', function() { // Check score assumptions expect(Scrabble.score('i')).toBe(1); expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('cat')).toBe(5); -// -// // Test the functionality -// expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); -// expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); -// expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); -// expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); -// }); + + // Test the functionality + expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); + expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); + expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); + expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); + }); }); // // describe('Player', function() { From b7b16cd6ebb195a4c774bb705880fce9d0e860cd Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 15:30:23 -0800 Subject: [PATCH 09/35] Added logic to throw exception if word is greater than 7 letters --- scrabble.js | 49 +++++++++++++++++++++++++------------------ spec/scrabble_spec.js | 9 ++++---- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/scrabble.js b/scrabble.js index 38d9d7f..23f4fb9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -15,41 +15,50 @@ const Scrabble = { let total = 0; let playedWord = word.toUpperCase(); - for (i=0; i < playedWord.length; i++) { - letter = playedWord[i]; + if (playedWord.length > 7) { + throw new UserException('Illegal play') + } - if (letter === null) { - total = 0; - } - total += tiles[letter]; + // for (i=0; i < playedWord.length; i++) { + // letter = playedWord[i]; + // + // if (letter === null) { + // total = 0; + // } + // + // total += tiles[letter]; + // + // } + // if ( word.length === 7 && total > 0 ) { + // total += 50; + // } + // return total; + // }, - } - if (word.length === 7 && total > 0) { + word.toUpperCase().split('').forEach(function(letter) { + total = tiles[letter] + total; + }); + if (word.length == 7 && total > 0) { total += 50; } return total; }, - // Question: Is forEach less efficient that forloop word.toUpperCase().split('').forEach(function(letter) { - // total = tiles[letter] + total; - // }); - // if (word.length == 7 && total > 0) { - // total += 50; - // } - // return total; - // }, - // TODO: add the highestScoreFrom method highestScoreFrom: function(arrayOfWords) { let max = 0; let highestScoreWord = ''; arrayOfWords.forEach(function(wrd) { - if ( Scrabble.score(wrd) > max ) { - max = Scrabble.score(wrd); + let wordScore = Scrabble.score(wrd) + let wordLength = wrd.length + if ( wordScore > max ) { + max = wordScore; + highestScoreWord = wrd; + } else if (( wordScore === max ) && ( wordLength === 7 )) { highestScoreWord = wrd; - } else if (( Scrabble.score(wrd) === max ) && (wrd.length < highestScoreWord.length)) { + } else if (( wordScore === max ) && ( wordLength < highestScoreWord.length )) { highestScoreWord = wrd; } }); diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 7e8cf33..826464a 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -27,10 +27,10 @@ describe('score', function() { expect(Scrabble.score('DoG')).toBe(5); }); -// it ('does not allow words > 7 letters', function() { -// expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); -// }); -// + it ('does not allow words > 7 letters', function() { + expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); + }); + // it ('does not allow empty words', function() { // expect(function() { Scrabble.score(''); }).toThrow(); // }); @@ -60,6 +60,7 @@ describe('highestScoreFrom', function() { expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); }); +// not passing // it ('if tied, prefer a word with 7 letters', function() { // const loser = 'zzzzzz'; // const winner = 'iiiiddd'; From 8fad408d496b8c9e918abb2c1409d7c5c417078e Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 15:34:20 -0800 Subject: [PATCH 10/35] Add logic to throw exception if an empty string is passed --- scrabble.js | 3 +++ spec/scrabble_spec.js | 12 ++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index 23f4fb9..6709cfd 100644 --- a/scrabble.js +++ b/scrabble.js @@ -19,6 +19,9 @@ const Scrabble = { throw new UserException('Illegal play') } + if (playedWord === '') { + throw new UserException('Illegal play') + } // for (i=0; i < playedWord.length; i++) { // letter = playedWord[i]; diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 826464a..0dc272a 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -31,11 +31,11 @@ describe('score', function() { expect(function() { Scrabble.score('abcdefgh'); }).toThrow(); }); -// it ('does not allow empty words', function() { -// expect(function() { Scrabble.score(''); }).toThrow(); -// }); -// }); -// + it ('does not allow empty words', function() { + expect(function() { Scrabble.score(''); }).toThrow(); + }); +}); + describe('highestScoreFrom', function() { it ('is defined', function() { expect(Scrabble.highestScoreFrom).toBeDefined(); @@ -226,4 +226,4 @@ describe('highestScoreFrom', function() { // expect(() => { player.highestWordScore() }).toThrow(); // }); // }); -}); +// }); From 024f84d539c678863734d6da5edb626766e2478e Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 15:43:11 -0800 Subject: [PATCH 11/35] Used regex to validate string --- scrabble.js | 5 +++++ spec/scrabble_spec.js | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index 6709cfd..7458bd3 100644 --- a/scrabble.js +++ b/scrabble.js @@ -12,6 +12,7 @@ const Scrabble = { // word.toUpperCase().split(' ').forEach(function(letter) { // console.log('this is the ' + word); + let regex = /^[a-zA-Z]+$/; let total = 0; let playedWord = word.toUpperCase(); @@ -23,6 +24,10 @@ const Scrabble = { throw new UserException('Illegal play') } + if (!playedWord.match(regex) ) { + throw new UserException('Illegal play') + } + // for (i=0; i < playedWord.length; i++) { // letter = playedWord[i]; // diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 0dc272a..e72d01b 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -15,11 +15,11 @@ describe('score', function() { expect(Scrabble.score('academy')).toBe(65); }); - // it ('throws on bad characters', function() { - // expect(function () { - // Scrabble.score('char^'); - // }).toThrow(); - // }); + it ('throws on bad characters', function() { + expect(function () { + Scrabble.score('char^'); + }).toThrow(); + }); it ('handles all upper- and lower-case letters', function() { expect(Scrabble.score('dog')).toBe(5); From 12ae92d8869307b34aa6b297a56e2b147dba75d1 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 21:43:01 -0800 Subject: [PATCH 12/35] Add constructor to Player class --- scrabble.js | 114 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 31 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7458bd3..2b3109e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,4 +1,4 @@ -let tiles = { +const tiles = { 'A': 1, 'E': 1, 'I': 1, 'O': 1, 'L': 1, 'N': 1, 'R': 1, 'U': 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, @@ -17,34 +17,34 @@ const Scrabble = { let playedWord = word.toUpperCase(); if (playedWord.length > 7) { - throw new UserException('Illegal play') + throw new UserException(`Illegal play, ${playedWord.length} exceeds allowed tile count`) } if (playedWord === '') { - throw new UserException('Illegal play') + throw new UserException('Illegal play, ' + playedWord + + ' contains bad characters') } if (!playedWord.match(regex) ) { - throw new UserException('Illegal play') + throw new UserException('Illegal play, sorry only words allowed') } + // for (i=0; i < playedWord.length; i++) { + // letter = playedWord[i]; + // + // if (letter === null) { + // total = 0; + // } + // + // total += tiles[letter]; + // + // } + // if ( word.length === 7 && total > 0 ) { + // total += 50; + // } + // return total; + // }, - // for (i=0; i < playedWord.length; i++) { - // letter = playedWord[i]; - // - // if (letter === null) { - // total = 0; - // } - // - // total += tiles[letter]; - // - // } - // if ( word.length === 7 && total > 0 ) { - // total += 50; - // } - // return total; - // }, - - word.toUpperCase().split('').forEach(function(letter) { + word.toUpperCase().split('').forEach(function(letter) { total = tiles[letter] + total; }); if (word.length == 7 && total > 0) { @@ -53,23 +53,45 @@ const Scrabble = { return total; }, - // TODO: add the highestScoreFrom method highestScoreFrom: function(arrayOfWords) { let max = 0; let highestScoreWord = ''; - arrayOfWords.forEach(function(wrd) { - let wordScore = Scrabble.score(wrd) - let wordLength = wrd.length + if (arrayOfWords.length === 0) { + throw new Error('No words to score'); + } + else if (arrayOfWords.length === 1){ + return arrayOfWords[0]; + } + + arrayOfWords.forEach(function(word) { + let wordScore = Scrabble.score(word) + // let wordLength = wrd.length if ( wordScore > max ) { max = wordScore; - highestScoreWord = wrd; - } else if (( wordScore === max ) && ( wordLength === 7 )) { - highestScoreWord = wrd; - } else if (( wordScore === max ) && ( wordLength < highestScoreWord.length )) { - highestScoreWord = wrd; + highestScoreWord = word; + } + else if ( wordScore === max ) { + console.log('MAX ' + max + ' and CURRENT SCORE is ' + wordScore); + if (word.length === 7) { + console.log('check if the current word is equal to ' + word.length); + console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); + highestScoreWord = word; + } + else if (word.length === highestScoreWord.length ) { + console.log('trying hard ' + highestScoreWord); + //highestScoreWord; + } + else if ( word.length < highestScoreWord.length) { + console.log('run this ' + highestScoreWord) + highestScoreWord = word; + } + // else { + // //highestScoreWord; + // } } }); + return highestScoreWord; } @@ -78,6 +100,36 @@ const Scrabble = { Scrabble.Player = class { // TODO: implement the Player class -}; + construtor(name) { + this.name = name; + } + // plays() { + // + // } + + // play(word) { + // + // } + // + // totalScore() { + // + // } + // + // hasWon() { + // + // } + // + // highestScoringWord() { + // + // } + // + // highestWordScore() { + // + // } + +}; // Scrabble.Player object + + + module.exports = Scrabble; From ee265ae5dc670799f74f656be2219d8b24464831 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 21:43:40 -0800 Subject: [PATCH 13/35] Passing test for Player function --- spec/scrabble_spec.js | 50 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index e72d01b..0ca0ea4 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -40,12 +40,12 @@ describe('highestScoreFrom', function() { it ('is defined', function() { expect(Scrabble.highestScoreFrom).toBeDefined(); }); -// -// it ('throws if no words were passed', function() { -// expect(function() { Scrabble.highestScoreFrom([]); }).toThrow(); -// expect(function() { Scrabble.highestScoreFrom('not array'); }).toThrow(); -// }); -// + + it ('throws if no words were passed', function() { + expect(function() { Scrabble.highestScoreFrom([]); }).toThrow(); + expect(function() { Scrabble.highestScoreFrom('not array'); }).toThrow(); + }); + it ('returns the only word in a length-1 array', function() { expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); }); @@ -60,19 +60,19 @@ describe('highestScoreFrom', function() { expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); }); -// not passing - // it ('if tied, prefer a word with 7 letters', function() { - // const loser = 'zzzzzz'; - // const winner = 'iiiiddd'; - // - // // Check score assumptions - // expect(Scrabble.score(loser)).toBe(60); - // expect(Scrabble.score(winner)).toBe(60); - // - // // Test functionality - // expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); - // expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); - // }); + + it ('if tied, prefer a word with 7 letters', function() { + const loser = 'zzzzzz'; + const winner = 'iiiiddd'; + + // Check score assumptions + expect(Scrabble.score(loser)).toBe(60); + expect(Scrabble.score(winner)).toBe(60); + + // Test functionality + expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); + // expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); + }); it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { // Check score assumptions @@ -97,11 +97,11 @@ describe('highestScoreFrom', function() { expect(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])).toBe('dog'); }); }); -// -// describe('Player', function() { -// it ('is defined', function() { -// expect(Scrabble.Player).toBeDefined(); -// }); + +describe('Player', function() { + it ('is defined', function() { + expect(Scrabble.Player).toBeDefined(); + }); // // describe('Constructor', function() { // it('Creates a new player', function() { @@ -113,7 +113,7 @@ describe('highestScoreFrom', function() { // it('Requires a name', function() { // expect(function() { new Scrabble.Player(); }).toThrow(); // }); -// }); + }); // // describe ('play', function() { // it ('Records the played word', function() { From 6f1de1e7182fece2fda3d6c3be7e71a7564390a8 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 22:24:31 -0800 Subject: [PATCH 14/35] Add error proof when name is undefined --- scrabble.js | 21 ++++++++++++++------- spec/scrabble_spec.js | 24 ++++++++++++------------ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/scrabble.js b/scrabble.js index 2b3109e..c0a8414 100644 --- a/scrabble.js +++ b/scrabble.js @@ -72,18 +72,18 @@ const Scrabble = { highestScoreWord = word; } else if ( wordScore === max ) { - console.log('MAX ' + max + ' and CURRENT SCORE is ' + wordScore); + // console.log('MAX ' + max + ' and CURRENT SCORE is ' + wordScore); if (word.length === 7) { - console.log('check if the current word is equal to ' + word.length); - console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); + // console.log('check if the current word is equal to ' + word.length); + // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); highestScoreWord = word; } else if (word.length === highestScoreWord.length ) { - console.log('trying hard ' + highestScoreWord); + // console.log('trying hard ' + highestScoreWord); //highestScoreWord; } else if ( word.length < highestScoreWord.length) { - console.log('run this ' + highestScoreWord) + // console.log('run this ' + highestScoreWord) highestScoreWord = word; } // else { @@ -99,8 +99,10 @@ const Scrabble = { Scrabble.Player = class { - // TODO: implement the Player class - construtor(name) { + constructor(name) { + if (name === undefined) { + throw new Error('No argument passed'); + } this.name = name; } // plays() { @@ -133,3 +135,8 @@ Scrabble.Player = class { module.exports = Scrabble; + +//test +let name = 'Bob'; +let player = new Scrabble.Player(name); +console.log(player.name) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 0ca0ea4..e1288a6 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -102,17 +102,17 @@ describe('Player', function() { it ('is defined', function() { expect(Scrabble.Player).toBeDefined(); }); -// -// describe('Constructor', function() { -// it('Creates a new player', function() { -// let name = 'test name'; -// let player = new Scrabble.Player(name); -// expect(player.name).toBe(name); -// }); -// -// it('Requires a name', function() { -// expect(function() { new Scrabble.Player(); }).toThrow(); -// }); + + describe('Constructor', function() { + it('Creates a new player', function() { + let name = 'test name'; + let player = new Scrabble.Player(name); + expect(player.name).toBe(name); + }); + + it('Requires a name', function() { + expect(function() { new Scrabble.Player(); }).toThrow(); + }); }); // // describe ('play', function() { @@ -226,4 +226,4 @@ describe('Player', function() { // expect(() => { player.highestWordScore() }).toThrow(); // }); // }); -// }); +}); From 7365134d1a389662763235db41e02fcc614eee01 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 22:32:46 -0800 Subject: [PATCH 15/35] Added plays property --- scrabble.js | 6 +++--- spec/scrabble_spec.js | 26 +++++++++++++------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/scrabble.js b/scrabble.js index c0a8414..fa88824 100644 --- a/scrabble.js +++ b/scrabble.js @@ -105,9 +105,9 @@ Scrabble.Player = class { } this.name = name; } - // plays() { - // - // } + plays() { + return plays + } // play(word) { // diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index e1288a6..6060000 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -114,18 +114,18 @@ describe('Player', function() { expect(function() { new Scrabble.Player(); }).toThrow(); }); }); -// -// describe ('play', function() { -// it ('Records the played word', function() { -// let word = 'dog'; -// let player = new Scrabble.Player('test player'); -// expect(player.plays.length).toBe(0); -// -// expect(player.play(word)).toBeTruthy(); -// -// expect(player.plays.length).toBe(1); -// expect(player.plays[0]).toBe(word); -// }); + + describe ('play', function() { + it ('Records the played word', function() { + let word = 'dog'; + let player = new Scrabble.Player('test player'); + expect(player.plays.length).toBe(0); + + // expect(player.play(word)).toBeTruthy(); + // + // expect(player.plays.length).toBe(1); + // expect(player.plays[0]).toBe(word); + }); // // it ('Requires a real word', function() { // let player = new Scrabble.Player('test player'); @@ -147,7 +147,7 @@ describe('Player', function() { // expect(player.play('dog')).toBe(false); // expect(player.plays.length).toBe(1); // }); -// }); + }); // // describe ('totalScore', function() { // it ('Is zero if the player has not played anything', function() { From 78ac5339f55cf92f112a45841fbad101e59a5c2f Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 23:25:02 -0800 Subject: [PATCH 16/35] Added plays arra --- scrabble.js | 24 ++++++++++++++++++------ spec/scrabble_spec.js | 36 ++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/scrabble.js b/scrabble.js index fa88824..bb1a221 100644 --- a/scrabble.js +++ b/scrabble.js @@ -103,15 +103,27 @@ Scrabble.Player = class { if (name === undefined) { throw new Error('No argument passed'); } - this.name = name; + this.name = name; // return player's name + this.plays = []; // store words played } - plays() { - return plays + play(word) { + if (word === undefined) { + throw new Error('Invalid word') + } + + this.plays.push(word) + return this.plays; } - // play(word) { - // - // } + // if total_score < 100 + // return false + // else + // return true + // end + // end + + + // // totalScore() { // diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 6060000..4943396 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -121,26 +121,26 @@ describe('Player', function() { let player = new Scrabble.Player('test player'); expect(player.plays.length).toBe(0); - // expect(player.play(word)).toBeTruthy(); + expect(player.play(word)).toBeTruthy(); + + expect(player.plays.length).toBe(1); + expect(player.plays[0]).toBe(word); + }); + + it ('Requires a real word', function() { + let player = new Scrabble.Player('test player'); + expect(player.plays.length).toBe(0); + + // expect(function() { player.play(); }).toThrow(); + // expect(player.plays.length).toBe(0); // - // expect(player.plays.length).toBe(1); - // expect(player.plays[0]).toBe(word); + // expect(function() { player.play(44); }).toThrow(); + // expect(player.plays.length).toBe(0); }); -// -// it ('Requires a real word', function() { -// let player = new Scrabble.Player('test player'); -// expect(player.plays.length).toBe(0); -// -// expect(function() { player.play(); }).toThrow(); -// expect(player.plays.length).toBe(0); -// -// expect(function() { player.play(44); }).toThrow(); -// expect(player.plays.length).toBe(0); -// }); -// -// it ('Returns false and does not update plays if the player has already won', function() { -// let player = new Scrabble.Player('test player'); -// expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts + + // it ('Returns false and does not update plays if the player has already won', function() { + // let player = new Scrabble.Player('test player'); + // expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts // expect(player.plays.length).toBe(1); // expect(player.hasWon()).toBeTruthy(); // From 23e9b9968eb37ea3becd7f67a1a1bf6a73990cfa Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Wed, 15 Nov 2017 23:37:24 -0800 Subject: [PATCH 17/35] Error handling for invalid word play --- scrabble.js | 6 ++++-- spec/scrabble_spec.js | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index bb1a221..83e2fcb 100644 --- a/scrabble.js +++ b/scrabble.js @@ -107,8 +107,10 @@ Scrabble.Player = class { this.plays = []; // store words played } play(word) { - if (word === undefined) { - throw new Error('Invalid word') + let regex = /^[a-zA-Z]+$/; + + if ((word === undefined) || (!word.match(regex))) { + throw new Error('Invalid word'); } this.plays.push(word) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 4943396..006c2d5 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -131,10 +131,10 @@ describe('Player', function() { let player = new Scrabble.Player('test player'); expect(player.plays.length).toBe(0); - // expect(function() { player.play(); }).toThrow(); - // expect(player.plays.length).toBe(0); - // - // expect(function() { player.play(44); }).toThrow(); + expect(function() { player.play(); }).toThrow(); + expect(player.plays.length).toBe(0); + + expect(function() { player.play(44); }).toThrow(); // expect(player.plays.length).toBe(0); }); From 9002acbb955490a63b6a97a7063c321af66a3577 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:00:47 -0800 Subject: [PATCH 18/35] Added totalScore function and test passed --- scrabble.js | 42 +++++++++++++++++++++++++++++------------- spec/scrabble_spec.js | 32 ++++++++++++++++---------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/scrabble.js b/scrabble.js index 83e2fcb..50da67d 100644 --- a/scrabble.js +++ b/scrabble.js @@ -117,19 +117,35 @@ Scrabble.Player = class { return this.plays; } - // if total_score < 100 - // return false - // else - // return true - // end - // end + // + def play(word) + // + @plays << word + // + total_score = 0 + // + @plays.each do |played_word| + // + total_score += Scoring.score(played_word) + // + end + // + if total_score >= 100 + // + false + // + else + // + return total_score + // + end + // +end + // + + + totalScore() { + let totalScore = 0; + this.plays.forEach(function(playedWord) { + totalScore += Scrabble.score(playedWord) + }); - // - // totalScore() { - // - // } + if (totalScore >= 100) { + return false; + } + else { + return totalScore; + } + } // // hasWon() { // @@ -151,6 +167,6 @@ Scrabble.Player = class { module.exports = Scrabble; //test -let name = 'Bob'; -let player = new Scrabble.Player(name); -console.log(player.name) +// let name = 'Bob'; +// let player = new Scrabble.Player(name); +// console.log(player.name) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 006c2d5..3fd0b15 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -135,25 +135,25 @@ describe('Player', function() { expect(player.plays.length).toBe(0); expect(function() { player.play(44); }).toThrow(); - // expect(player.plays.length).toBe(0); + expect(player.plays.length).toBe(0); }); - // it ('Returns false and does not update plays if the player has already won', function() { - // let player = new Scrabble.Player('test player'); - // expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts -// expect(player.plays.length).toBe(1); -// expect(player.hasWon()).toBeTruthy(); -// -// expect(player.play('dog')).toBe(false); -// expect(player.plays.length).toBe(1); -// }); + it ('Returns false and does not update plays if the player has already won', function() { + let player = new Scrabble.Player('test player'); + expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts + expect(player.plays.length).toBe(1); + // expect(player.hasWon()).toBeTruthy(); + + // expect(player.play('dog')).toBe(false); + // expect(player.plays.length).toBe(1); + }); }); // -// describe ('totalScore', function() { -// it ('Is zero if the player has not played anything', function() { -// let player = new Scrabble.Player('test player'); -// expect(player.totalScore()).toBe(0); -// }); + describe ('totalScore', function() { + it ('Is zero if the player has not played anything', function() { + let player = new Scrabble.Player('test player'); + expect(player.totalScore()).toBe(0); + }); // // it ('Is updated by play', function() { // let player = new Scrabble.Player('test player'); @@ -165,7 +165,7 @@ describe('Player', function() { // player.play('goat'); // expect(player.totalScore()).toBe(15); // }); -// }); + }); // // describe('hasWon', function() { // it('returns false when score < 100', function() { From 3bd8d7160a182bd76610a54b8c838d9051c7e2cb Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:02:04 -0800 Subject: [PATCH 19/35] Passing tests for totalScore function returning the correct scores --- spec/scrabble_spec.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 3fd0b15..cbe638d 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -154,17 +154,17 @@ describe('Player', function() { let player = new Scrabble.Player('test player'); expect(player.totalScore()).toBe(0); }); -// -// it ('Is updated by play', function() { -// let player = new Scrabble.Player('test player'); -// expect(player.totalScore()).toBe(0); -// player.play('dog'); -// expect(player.totalScore()).toBe(5); -// player.play('cat'); -// expect(player.totalScore()).toBe(10); -// player.play('goat'); -// expect(player.totalScore()).toBe(15); -// }); + + it ('Is updated by play', function() { + let player = new Scrabble.Player('test player'); + expect(player.totalScore()).toBe(0); + player.play('dog'); + expect(player.totalScore()).toBe(5); + player.play('cat'); + expect(player.totalScore()).toBe(10); + player.play('goat'); + expect(player.totalScore()).toBe(15); + }); }); // // describe('hasWon', function() { From fd877194372e66799169a4289bd729c7cbfe95d3 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:19:02 -0800 Subject: [PATCH 20/35] Moved functionality for checking if player won from totalScore function to hasWon function --- scrabble.js | 14 +++++++------- spec/scrabble_spec.js | 32 ++++++++++++++++---------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/scrabble.js b/scrabble.js index 50da67d..32f79bc 100644 --- a/scrabble.js +++ b/scrabble.js @@ -138,18 +138,18 @@ Scrabble.Player = class { this.plays.forEach(function(playedWord) { totalScore += Scrabble.score(playedWord) }); + return totalScore + } - if (totalScore >= 100) { - return false; + hasWon() { + if (this.totalScore() < 100) { + return false } else { - return totalScore; + return true } } - // - // hasWon() { - // - // } + // // highestScoringWord() { // diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index cbe638d..3db9a4f 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -166,21 +166,21 @@ describe('Player', function() { expect(player.totalScore()).toBe(15); }); }); -// -// describe('hasWon', function() { -// it('returns false when score < 100', function() { -// let player = new Scrabble.Player('test player'); -// expect(player.totalScore()).toBe(0); -// expect(player.hasWon()).toBe(false); -// -// player.play('zzzzz'); // Score += 50 -// expect(player.totalScore()).toBe(50); -// expect(player.hasWon()).toBe(false); -// -// player.play('zzzzji'); // Score += 49 -// expect(player.totalScore()).toBe(99); -// expect(player.hasWon()).toBe(false); -// }); + + describe('hasWon', function() { + it('returns false when score < 100', function() { + let player = new Scrabble.Player('test player'); + expect(player.totalScore()).toBe(0); + expect(player.hasWon()).toBe(false); + + // player.play('zzzzz'); // Score += 50 + // expect(player.totalScore()).toBe(50); + // expect(player.hasWon()).toBe(false); + // + // player.play('zzzzji'); // Score += 49 + // expect(player.totalScore()).toBe(99); + // expect(player.hasWon()).toBe(false); + }); // // it('returns true when score == 100', function() { // let player = new Scrabble.Player('test player'); @@ -195,7 +195,7 @@ describe('Player', function() { // expect(player.totalScore()).toBe(120); // expect(player.hasWon()).toBe(true); // }); -// }); + }); // // describe('highestScoringWord', function() { // // Tie-breaking logic is already described in the tests From db29d0e632ae146b224681d043c387f226e37a50 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:21:52 -0800 Subject: [PATCH 21/35] All tests for hasWon function passing --- spec/scrabble_spec.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 3db9a4f..c8d9ab2 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -173,28 +173,28 @@ describe('Player', function() { expect(player.totalScore()).toBe(0); expect(player.hasWon()).toBe(false); - // player.play('zzzzz'); // Score += 50 - // expect(player.totalScore()).toBe(50); - // expect(player.hasWon()).toBe(false); - // - // player.play('zzzzji'); // Score += 49 - // expect(player.totalScore()).toBe(99); - // expect(player.hasWon()).toBe(false); + player.play('zzzzz'); // Score += 50 + expect(player.totalScore()).toBe(50); + expect(player.hasWon()).toBe(false); + + player.play('zzzzji'); // Score += 49 + expect(player.totalScore()).toBe(99); + expect(player.hasWon()).toBe(false); + }); + + it('returns true when score == 100', function() { + let player = new Scrabble.Player('test player'); + player.play('zzzzbbf'); // Score += 100 + expect(player.totalScore()).toBe(100); + expect(player.hasWon()).toBe(true); + }); + + it('returns true when score > 100', function() { + let player = new Scrabble.Player('test player'); + player.play('zzzzzzz'); // Score += 120 + expect(player.totalScore()).toBe(120); + expect(player.hasWon()).toBe(true); }); -// -// it('returns true when score == 100', function() { -// let player = new Scrabble.Player('test player'); -// player.play('zzzzbbf'); // Score += 100 -// expect(player.totalScore()).toBe(100); -// expect(player.hasWon()).toBe(true); -// }); -// -// it('returns true when score > 100', function() { -// let player = new Scrabble.Player('test player'); -// player.play('zzzzzzz'); // Score += 120 -// expect(player.totalScore()).toBe(120); -// expect(player.hasWon()).toBe(true); -// }); }); // // describe('highestScoringWord', function() { From d04984b505031f2d79bd537e074fe99ecf459822 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:39:39 -0800 Subject: [PATCH 22/35] Tests pass for checking if player has won --- scrabble.js | 26 +++++++------------------- spec/scrabble_spec.js | 6 +++--- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/scrabble.js b/scrabble.js index 32f79bc..26d8181 100644 --- a/scrabble.js +++ b/scrabble.js @@ -28,21 +28,6 @@ const Scrabble = { if (!playedWord.match(regex) ) { throw new UserException('Illegal play, sorry only words allowed') } - // for (i=0; i < playedWord.length; i++) { - // letter = playedWord[i]; - // - // if (letter === null) { - // total = 0; - // } - // - // total += tiles[letter]; - // - // } - // if ( word.length === 7 && total > 0 ) { - // total += 50; - // } - // return total; - // }, word.toUpperCase().split('').forEach(function(letter) { total = tiles[letter] + total; @@ -109,6 +94,10 @@ Scrabble.Player = class { play(word) { let regex = /^[a-zA-Z]+$/; + if (this.hasWon()) { + return false; + } + if ((word === undefined) || (!word.match(regex))) { throw new Error('Invalid word'); } @@ -134,11 +123,11 @@ Scrabble.Player = class { // + totalScore() { - let totalScore = 0; + let total = 0; this.plays.forEach(function(playedWord) { - totalScore += Scrabble.score(playedWord) + total += Scrabble.score(playedWord) }); - return totalScore + return total } hasWon() { @@ -150,7 +139,6 @@ Scrabble.Player = class { } } - // // highestScoringWord() { // // } diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c8d9ab2..0ff9f83 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -142,10 +142,10 @@ describe('Player', function() { let player = new Scrabble.Player('test player'); expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts expect(player.plays.length).toBe(1); - // expect(player.hasWon()).toBeTruthy(); + expect(player.hasWon()).toBeTruthy(); - // expect(player.play('dog')).toBe(false); - // expect(player.plays.length).toBe(1); + expect(player.play('dog')).toBe(false); + expect(player.plays.length).toBe(1); }); }); // From 450bac2930c5d8a040d50c74e4252ae83a790e84 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:50:13 -0800 Subject: [PATCH 23/35] Added highestScoringWord function --- scrabble.js | 26 +++++--------------------- spec/scrabble_spec.js | 22 +++++++++++----------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/scrabble.js b/scrabble.js index 26d8181..799e7a3 100644 --- a/scrabble.js +++ b/scrabble.js @@ -97,7 +97,7 @@ Scrabble.Player = class { if (this.hasWon()) { return false; } - + if ((word === undefined) || (!word.match(regex))) { throw new Error('Invalid word'); } @@ -106,22 +106,6 @@ Scrabble.Player = class { return this.plays; } - - - // + def play(word) - // + @plays << word - // + total_score = 0 - // + @plays.each do |played_word| - // + total_score += Scoring.score(played_word) - // + end - // + if total_score >= 100 - // + false - // + else - // + return total_score - // + end - // +end - // + - totalScore() { let total = 0; this.plays.forEach(function(playedWord) { @@ -139,10 +123,10 @@ Scrabble.Player = class { } } - // highestScoringWord() { - // - // } - // + highestScoringWord() { + return Scrabble.highestScoreFrom(this.plays) + } + // highestWordScore() { // // } diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 0ff9f83..d910a28 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -196,22 +196,22 @@ describe('Player', function() { expect(player.hasWon()).toBe(true); }); }); -// -// describe('highestScoringWord', function() { -// // Tie-breaking logic is already described in the tests -// // for highestWordFrom, so we will not repeat it here. -// it('returns the highest scoring word played', function() { -// let player = new Scrabble.Player('test player'); -// player.play('cat'); -// player.play('zzzz'); -// expect(player.highestScoringWord()).toBe('zzzz'); -// }); + + describe('highestScoringWord', function() { + // Tie-breaking logic is already described in the tests + // for highestWordFrom, so we will not repeat it here. + it('returns the highest scoring word played', function() { + let player = new Scrabble.Player('test player'); + player.play('cat'); + player.play('zzzz'); + expect(player.highestScoringWord()).toBe('zzzz'); + }); // // it('throws an error if no words have been played', function() { // let player = new Scrabble.Player('test player'); // expect(() => { player.highestScoringWord() }).toThrow(); // }); -// }); + }); // // describe('highestWordScore', function() { // it('returns the score of the highest scoring word played', function() { From 36b9b0597652d279f84940f904dc9e622564dc81 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:51:40 -0800 Subject: [PATCH 24/35] All tests passing for highestScoringWord function --- spec/scrabble_spec.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index d910a28..f7807b8 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -206,11 +206,11 @@ describe('Player', function() { player.play('zzzz'); expect(player.highestScoringWord()).toBe('zzzz'); }); -// -// it('throws an error if no words have been played', function() { -// let player = new Scrabble.Player('test player'); -// expect(() => { player.highestScoringWord() }).toThrow(); -// }); + + it('throws an error if no words have been played', function() { + let player = new Scrabble.Player('test player'); + expect(() => { player.highestScoringWord() }).toThrow(); + }); }); // // describe('highestWordScore', function() { From 3982f51f6ffcd2a080e20a66162dcf43fc25ba5e Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:58:04 -0800 Subject: [PATCH 25/35] Added logic for highest word score --- scrabble.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 799e7a3..9eb8919 100644 --- a/scrabble.js +++ b/scrabble.js @@ -127,9 +127,9 @@ Scrabble.Player = class { return Scrabble.highestScoreFrom(this.plays) } - // highestWordScore() { - // - // } + highestWordScore() { + return Scrabble.score(this.highestScoringWord()) + } }; // Scrabble.Player object From f5229814f8ecccb97882536f88a330a756d29686 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 00:58:20 -0800 Subject: [PATCH 26/35] All tests for highest word score passing --- spec/scrabble_spec.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index f7807b8..c866869 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -212,18 +212,18 @@ describe('Player', function() { expect(() => { player.highestScoringWord() }).toThrow(); }); }); -// -// describe('highestWordScore', function() { -// it('returns the score of the highest scoring word played', function() { -// let player = new Scrabble.Player('test player'); -// player.play('cat'); -// player.play('zzzz'); -// expect(player.highestWordScore()).toBe(40); -// }); -// -// it('throws an error if no words have been played', function() { -// let player = new Scrabble.Player('test player'); -// expect(() => { player.highestWordScore() }).toThrow(); -// }); -// }); + + describe('highestWordScore', function() { + it('returns the score of the highest scoring word played', function() { + let player = new Scrabble.Player('test player'); + player.play('cat'); + player.play('zzzz'); + expect(player.highestWordScore()).toBe(40); + }); + + it('throws an error if no words have been played', function() { + let player = new Scrabble.Player('test player'); + expect(() => { player.highestWordScore() }).toThrow(); + }); + }); }); From 86c10bd6fce79867783ffb305a926f57ad2c5736 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 01:27:17 -0800 Subject: [PATCH 27/35] Woo hoo fixed the scoring bug, every test passes --- scrabble.js | 22 ++++++++++------------ spec/scrabble_spec.js | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index 9eb8919..4d8b8a1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -8,9 +8,6 @@ const tiles = { const Scrabble = { score: function(word) { - // TODO: implement score - // word.toUpperCase().split(' ').forEach(function(letter) { - // console.log('this is the ' + word); let regex = /^[a-zA-Z]+$/; let total = 0; @@ -51,23 +48,24 @@ const Scrabble = { arrayOfWords.forEach(function(word) { let wordScore = Scrabble.score(word) - // let wordLength = wrd.length + let wordLength = word.length + if ( wordScore > max ) { max = wordScore; highestScoreWord = word; - } - else if ( wordScore === max ) { + } else if ( wordScore === max ) { // console.log('MAX ' + max + ' and CURRENT SCORE is ' + wordScore); - if (word.length === 7) { + if ( wordLength === 7 ) { // console.log('check if the current word is equal to ' + word.length); // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); highestScoreWord = word; - } - else if (word.length === highestScoreWord.length ) { + } else if (highestScoreWord.length === 7 ) { // console.log('trying hard ' + highestScoreWord); - //highestScoreWord; - } - else if ( word.length < highestScoreWord.length) { + highestScoreWord; + } else if (wordLength === highestScoreWord.length ) { + // console.log('trying hard ' + highestScoreWord); + highestScoreWord; + } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { // console.log('run this ' + highestScoreWord) highestScoreWord = word; } diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c866869..da5e274 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -71,7 +71,7 @@ describe('highestScoreFrom', function() { // Test functionality expect(Scrabble.highestScoreFrom([loser, winner])).toBe(winner); - // expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); + expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); }); it ('if tied and no word has 7 letters, prefers the word with fewer letters', function() { From 06cb109380955d50aac8d79622a54483e818d3b4 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 01:31:40 -0800 Subject: [PATCH 28/35] Formatting code --- scrabble.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/scrabble.js b/scrabble.js index 4d8b8a1..c8a3108 100644 --- a/scrabble.js +++ b/scrabble.js @@ -54,30 +54,23 @@ const Scrabble = { max = wordScore; highestScoreWord = word; } else if ( wordScore === max ) { - // console.log('MAX ' + max + ' and CURRENT SCORE is ' + wordScore); if ( wordLength === 7 ) { - // console.log('check if the current word is equal to ' + word.length); // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); highestScoreWord = word; } else if (highestScoreWord.length === 7 ) { - // console.log('trying hard ' + highestScoreWord); + // console.log('work ' + highestScoreWord); highestScoreWord; } else if (wordLength === highestScoreWord.length ) { - // console.log('trying hard ' + highestScoreWord); + // console.log('work ' + highestScoreWord); highestScoreWord; } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { - // console.log('run this ' + highestScoreWord) highestScoreWord = word; } - // else { - // //highestScoreWord; - // } } }); return highestScoreWord; } - }; // end of Scrabble function @@ -128,7 +121,6 @@ Scrabble.Player = class { highestWordScore() { return Scrabble.score(this.highestScoringWord()) } - }; // Scrabble.Player object @@ -136,7 +128,7 @@ Scrabble.Player = class { module.exports = Scrabble; -//test +//tests // let name = 'Bob'; // let player = new Scrabble.Player(name); // console.log(player.name) From 3f9f85fc71aa3c518d13dc933f16d52ba95b549c Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 01:46:19 -0800 Subject: [PATCH 29/35] Fixed indentation & semi-colons --- scrabble.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index c8a3108..d2bb191 100644 --- a/scrabble.js +++ b/scrabble.js @@ -14,16 +14,16 @@ const Scrabble = { let playedWord = word.toUpperCase(); if (playedWord.length > 7) { - throw new UserException(`Illegal play, ${playedWord.length} exceeds allowed tile count`) + throw new UserException(`Illegal play, ${playedWord.length} exceeds allowed tile count`); } if (playedWord === '') { - throw new UserException('Illegal play, ' + playedWord - + ' contains bad characters') + throw new UserException(`Illegal play ${playedWord} + contains bad characters`); } if (!playedWord.match(regex) ) { - throw new UserException('Illegal play, sorry only words allowed') + throw new UserException('Illegal play, sorry only words allowed'); } word.toUpperCase().split('').forEach(function(letter) { @@ -41,8 +41,7 @@ const Scrabble = { if (arrayOfWords.length === 0) { throw new Error('No words to score'); - } - else if (arrayOfWords.length === 1){ + } else if (arrayOfWords.length === 1){ return arrayOfWords[0]; } @@ -82,6 +81,7 @@ Scrabble.Player = class { this.name = name; // return player's name this.plays = []; // store words played } + play(word) { let regex = /^[a-zA-Z]+$/; @@ -102,24 +102,23 @@ Scrabble.Player = class { this.plays.forEach(function(playedWord) { total += Scrabble.score(playedWord) }); - return total + return total; } hasWon() { if (this.totalScore() < 100) { - return false - } - else { - return true + return false; + } else { + return true; } } highestScoringWord() { - return Scrabble.highestScoreFrom(this.plays) + return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { - return Scrabble.score(this.highestScoringWord()) + return Scrabble.score(this.highestScoringWord()); } }; // Scrabble.Player object From 370753e2430f31ac58cbfebbfadd134658233d20 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 01:52:42 -0800 Subject: [PATCH 30/35] Format code --- scrabble.js | 174 ++++++++++++++++++++++++++-------------------------- 1 file changed, 86 insertions(+), 88 deletions(-) diff --git a/scrabble.js b/scrabble.js index d2bb191..398db58 100644 --- a/scrabble.js +++ b/scrabble.js @@ -19,115 +19,113 @@ const Scrabble = { if (playedWord === '') { throw new UserException(`Illegal play ${playedWord} - contains bad characters`); - } + contains bad characters`); + } - if (!playedWord.match(regex) ) { - throw new UserException('Illegal play, sorry only words allowed'); - } + if (!playedWord.match(regex) ) { + throw new UserException('Illegal play, sorry only words allowed'); + } - word.toUpperCase().split('').forEach(function(letter) { - total = tiles[letter] + total; - }); - if (word.length == 7 && total > 0) { - total += 50; - } - return total; - }, + word.toUpperCase().split('').forEach(function(letter) { + total = tiles[letter] + total; + }); + if (word.length == 7 && total > 0) { + total += 50; + } + return total; + }, - highestScoreFrom: function(arrayOfWords) { - let max = 0; - let highestScoreWord = ''; + highestScoreFrom: function(arrayOfWords) { + let max = 0; + let highestScoreWord = ''; - if (arrayOfWords.length === 0) { - throw new Error('No words to score'); - } else if (arrayOfWords.length === 1){ - return arrayOfWords[0]; - } + if (arrayOfWords.length === 0) { + throw new Error('No words to score'); + } else if (arrayOfWords.length === 1){ + return arrayOfWords[0]; + } - arrayOfWords.forEach(function(word) { - let wordScore = Scrabble.score(word) - let wordLength = word.length + arrayOfWords.forEach(function(word) { + let wordScore = Scrabble.score(word) + let wordLength = word.length - if ( wordScore > max ) { - max = wordScore; - highestScoreWord = word; - } else if ( wordScore === max ) { - if ( wordLength === 7 ) { - // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); - highestScoreWord = word; - } else if (highestScoreWord.length === 7 ) { - // console.log('work ' + highestScoreWord); - highestScoreWord; - } else if (wordLength === highestScoreWord.length ) { - // console.log('work ' + highestScoreWord); - highestScoreWord; - } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { + if ( wordScore > max ) { + max = wordScore; highestScoreWord = word; + } else if ( wordScore === max ) { + if ( wordLength === 7 ) { + // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); + return highestScoreWord = word; + } else if (highestScoreWord.length === 7 ) { + // console.log('work ' + highestScoreWord); + return highestScoreWord; + } else if (wordLength === highestScoreWord.length ) { + // console.log('work ' + highestScoreWord); + return highestScoreWord; + } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { + return highestScoreWord = word; + } } - } - }); - - return highestScoreWord; - } -}; // end of Scrabble function + }); + return highestScoreWord; + } + }; // end of Scrabble function -Scrabble.Player = class { - constructor(name) { - if (name === undefined) { - throw new Error('No argument passed'); + Scrabble.Player = class { + constructor(name) { + if (name === undefined) { + throw new Error('No argument passed'); + } + this.name = name; // return player's name + this.plays = []; // store words played } - this.name = name; // return player's name - this.plays = []; // store words played - } - play(word) { - let regex = /^[a-zA-Z]+$/; + play(word) { + let regex = /^[a-zA-Z]+$/; - if (this.hasWon()) { - return false; - } + if (this.hasWon()) { + return false; + } - if ((word === undefined) || (!word.match(regex))) { - throw new Error('Invalid word'); - } + if ((word === undefined) || (!word.match(regex))) { + throw new Error('Invalid word'); + } - this.plays.push(word) - return this.plays; - } + this.plays.push(word) + return this.plays; + } - totalScore() { - let total = 0; - this.plays.forEach(function(playedWord) { - total += Scrabble.score(playedWord) - }); - return total; - } - - hasWon() { - if (this.totalScore() < 100) { - return false; - } else { - return true; + totalScore() { + let total = 0; + this.plays.forEach(function(playedWord) { + total += Scrabble.score(playedWord) + }); + return total; } - } - highestScoringWord() { - return Scrabble.highestScoreFrom(this.plays); - } + hasWon() { + if (this.totalScore() < 100) { + return false; + } else { + return true; + } + } - highestWordScore() { - return Scrabble.score(this.highestScoringWord()); - } -}; // Scrabble.Player object + highestScoringWord() { + return Scrabble.highestScoreFrom(this.plays); + } + highestWordScore() { + return Scrabble.score(this.highestScoringWord()); + } + }; // Scrabble.Player object -module.exports = Scrabble; + module.exports = Scrabble; -//tests -// let name = 'Bob'; -// let player = new Scrabble.Player(name); -// console.log(player.name) + //tests + // let name = 'Bob'; + // let player = new Scrabble.Player(name); + // console.log(player.name) From 51f48c89af7bc3250673e6e95e2f98f4d65eba0a Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 17:34:46 -0800 Subject: [PATCH 31/35] Added stub for arrow function --- scrabble.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scrabble.js b/scrabble.js index 398db58..4d3e362 100644 --- a/scrabble.js +++ b/scrabble.js @@ -104,6 +104,16 @@ const Scrabble = { return total; } + //Arrow function not working + // totalScore() { + // // let total = 0; + // // let total = this.plays.reduce((total, playedWord) => total + Scrabble.score(playedWord)); + // // console.log('hello' + playedWord); + // + // this.plays.reduce((total, playedWord) => ( total + Scrabble.score(playedWord), 0); + // }; + // //); + hasWon() { if (this.totalScore() < 100) { return false; @@ -121,6 +131,10 @@ const Scrabble = { } }; // Scrabble.Player object +// Optional Enhancements +// Scrabble.TileBag = class { +// +// } module.exports = Scrabble; From 9be034dbf78dd35ecb9338487b34fb53336e83f2 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 17:40:00 -0800 Subject: [PATCH 32/35] Completed final revisions --- scrabble.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scrabble.js b/scrabble.js index 7a1f161..552b26a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -9,6 +9,8 @@ const Scrabble = { Scrabble.Player = class { // TODO: implement the Player class + + // TODO: add arrow function }; module.exports = Scrabble; From 38e59359c8c0b49f1ce3ef62cc56481b72f25c44 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 20:47:07 -0800 Subject: [PATCH 33/35] revision complete --- scrabble.js | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/scrabble.js b/scrabble.js index 4d3e362..ece8b23 100644 --- a/scrabble.js +++ b/scrabble.js @@ -96,24 +96,19 @@ const Scrabble = { return this.plays; } + // totalScore() { + // let total = 0; + // this.plays.forEach(function(playedWord) { + // total += Scrabble.score(playedWord) + // }); + // return total; + // } + + // arrow function totalScore() { - let total = 0; - this.plays.forEach(function(playedWord) { - total += Scrabble.score(playedWord) - }); - return total; + return this.plays.reduce((total, playedWord) => { return total + Scrabble.score(playedWord) }, 0); } - //Arrow function not working - // totalScore() { - // // let total = 0; - // // let total = this.plays.reduce((total, playedWord) => total + Scrabble.score(playedWord)); - // // console.log('hello' + playedWord); - // - // this.plays.reduce((total, playedWord) => ( total + Scrabble.score(playedWord), 0); - // }; - // //); - hasWon() { if (this.totalScore() < 100) { return false; @@ -131,10 +126,10 @@ const Scrabble = { } }; // Scrabble.Player object -// Optional Enhancements -// Scrabble.TileBag = class { -// -// } + // Optional Enhancements + // Scrabble.TileBag = class { + // + // } module.exports = Scrabble; From bfa5f122ff886f9071783b9967eb838d29c92661 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 20:50:10 -0800 Subject: [PATCH 34/35] submit final changes --- scrabble.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/scrabble.js b/scrabble.js index 67aa462..ece8b23 100644 --- a/scrabble.js +++ b/scrabble.js @@ -17,7 +17,6 @@ const Scrabble = { throw new UserException(`Illegal play, ${playedWord.length} exceeds allowed tile count`); } -<<<<<<< HEAD if (playedWord === '') { throw new UserException(`Illegal play ${playedWord} contains bad characters`); @@ -134,13 +133,6 @@ const Scrabble = { module.exports = Scrabble; -======= -Scrabble.Player = class { - // TODO: implement the Player class - - // TODO: add arrow function -}; ->>>>>>> 9be034dbf78dd35ecb9338487b34fb53336e83f2 //tests // let name = 'Bob'; From 9cab762d50dc1a3c544aadffdd6294aa10b07ac5 Mon Sep 17 00:00:00 2001 From: nkiru onwuneme Date: Thu, 16 Nov 2017 21:03:15 -0800 Subject: [PATCH 35/35] fixed indentation --- scrabble.js | 226 ++++++++++++++++++++++++++++------------------------ 1 file changed, 122 insertions(+), 104 deletions(-) diff --git a/scrabble.js b/scrabble.js index ece8b23..f98418e 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,14 +1,34 @@ const tiles = { - 'A': 1, 'E': 1, 'I': 1, 'O': 1, 'L': 1, 'N': 1, 'R': 1, - 'U': 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 + 'A': 1, + 'E': 1, + 'I': 1, + 'O': 1, + 'L': 1, + 'N': 1, + 'R': 1, + 'U': 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 }; - const Scrabble = { score: function(word) { - let regex = /^[a-zA-Z]+$/; let total = 0; let playedWord = word.toUpperCase(); @@ -18,123 +38,121 @@ const Scrabble = { } if (playedWord === '') { - throw new UserException(`Illegal play ${playedWord} - contains bad characters`); - } - - if (!playedWord.match(regex) ) { - throw new UserException('Illegal play, sorry only words allowed'); - } - - word.toUpperCase().split('').forEach(function(letter) { - total = tiles[letter] + total; - }); - if (word.length == 7 && total > 0) { - total += 50; - } - return total; - }, + throw new UserException(`Illegal play ${playedWord} contains bad characters`); + } - highestScoreFrom: function(arrayOfWords) { - let max = 0; - let highestScoreWord = ''; + if (!playedWord.match(regex) ) { + throw new UserException('Illegal play, sorry only words allowed'); + } - if (arrayOfWords.length === 0) { - throw new Error('No words to score'); - } else if (arrayOfWords.length === 1){ - return arrayOfWords[0]; - } + word.toUpperCase().split('').forEach(function(letter) { + total = tiles[letter] + total; + }); - arrayOfWords.forEach(function(word) { - let wordScore = Scrabble.score(word) - let wordLength = word.length - - if ( wordScore > max ) { - max = wordScore; - highestScoreWord = word; - } else if ( wordScore === max ) { - if ( wordLength === 7 ) { - // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); - return highestScoreWord = word; - } else if (highestScoreWord.length === 7 ) { - // console.log('work ' + highestScoreWord); - return highestScoreWord; - } else if (wordLength === highestScoreWord.length ) { - // console.log('work ' + highestScoreWord); - return highestScoreWord; - } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { - return highestScoreWord = word; - } - } - }); - return highestScoreWord; + if (word.length == 7 && total > 0) { + total += 50; } - }; // end of Scrabble function + return total; + }, + highestScoreFrom: function(arrayOfWords) { + let max = 0; + let highestScoreWord = ''; - Scrabble.Player = class { - constructor(name) { - if (name === undefined) { - throw new Error('No argument passed'); - } - this.name = name; // return player's name - this.plays = []; // store words played + if (arrayOfWords.length === 0) { + throw new Error('No words to score'); + } else if (arrayOfWords.length === 1){ + return arrayOfWords[0]; } - play(word) { - let regex = /^[a-zA-Z]+$/; - - if (this.hasWon()) { - return false; + arrayOfWords.forEach(function(word) { + let wordScore = Scrabble.score(word) + let wordLength = word.length + + if ( wordScore > max ) { + max = wordScore; + highestScoreWord = word; + } else if ( wordScore === max ) { + if ( wordLength === 7 ) { + // console.log('HIGHEST ' + highestScoreWord + ' VALUE OF CURRENT WORD ' + word); + return highestScoreWord = word; + } else if (highestScoreWord.length === 7 ) { + // console.log('work ' + highestScoreWord); + return highestScoreWord; + } else if (wordLength === highestScoreWord.length ) { + // console.log('work ' + highestScoreWord); + return highestScoreWord; + } else if (( wordLength < highestScoreWord.length) && ( wordLength !== 7)) { + return highestScoreWord = word; + } } + }); + return highestScoreWord; + } +}; // end of Scrabble function - if ((word === undefined) || (!word.match(regex))) { - throw new Error('Invalid word'); - } - this.plays.push(word) - return this.plays; +Scrabble.Player = class { + constructor(name) { + if (name === undefined) { + throw new Error('No argument passed'); } + this.name = name; // return player's name + this.plays = []; // store words played + } - // totalScore() { - // let total = 0; - // this.plays.forEach(function(playedWord) { - // total += Scrabble.score(playedWord) - // }); - // return total; - // } - - // arrow function - totalScore() { - return this.plays.reduce((total, playedWord) => { return total + Scrabble.score(playedWord) }, 0); - } + play(word) { + let regex = /^[a-zA-Z]+$/; - hasWon() { - if (this.totalScore() < 100) { - return false; - } else { - return true; - } + if (this.hasWon()) { + return false; } - highestScoringWord() { - return Scrabble.highestScoreFrom(this.plays); + if ((word === undefined) || (!word.match(regex))) { + throw new Error('Invalid word'); } - highestWordScore() { - return Scrabble.score(this.highestScoringWord()); - } - }; // Scrabble.Player object + this.plays.push(word) + return this.plays; + } - // Optional Enhancements - // Scrabble.TileBag = class { - // + // totalScore() { + // let total = 0; + // this.plays.forEach(function(playedWord) { + // total += Scrabble.score(playedWord) + // }); + // return total; // } + // arrow function + totalScore() { + return this.plays.reduce((total, playedWord) => { return total + Scrabble.score(playedWord) }, 0); + } - module.exports = Scrabble; - - //tests - // let name = 'Bob'; - // let player = new Scrabble.Player(name); - // console.log(player.name) + hasWon() { + if (this.totalScore() < 100) { + return false; + } else { + return true; + } + } + + highestScoringWord() { + return Scrabble.highestScoreFrom(this.plays); + } + + highestWordScore() { + return Scrabble.score(this.highestScoringWord()); + } +}; // Scrabble.Player object + +// Optional Enhancements +// Scrabble.TileBag = class { +// +// } +module.exports = Scrabble; + +//tests +// let name = 'Bob'; +// let player = new Scrabble.Player(name); +// console.log(player.name)