From fe473b5170ba40daa6cde5c8ff5eb1a4a10d16da Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 14:44:06 -0800 Subject: [PATCH 01/28] Run NPM Install --- package-lock.json | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) 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=" + } + } +} From dc32db54f72f05baccee6c0abc32981367661a9a Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 15:38:41 -0800 Subject: [PATCH 02/28] Write scoring function --- scrabble.js | 19 ++- spec/scrabble_spec.js | 380 +++++++++++++++++++++--------------------- 2 files changed, 206 insertions(+), 193 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7a1f161..2833c4b 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,11 +1,24 @@ +const letterPoints = { + A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8, Y: 4, Z: 10 +}; + + +let total = 0; + + const Scrabble = { score: function(word) { - // TODO: implement score - } + + for(let i = 0; i < word.length; i++){ + total += letterPoints[word[i]]; + } // end for loop + return total; + + } //end function word // TODO: add the highestScoreFrom method -}; +}; //end const Scrabble Scrabble.Player = class { // TODO: implement the Player class diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..19abc80 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -36,193 +36,193 @@ describe('score', function() { }); }); -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(); - }); - }); -}); +// 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 91962ce2cccf7be7c4a522e8e11df2a580b9c148 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 15:50:46 -0800 Subject: [PATCH 03/28] Get basic scoring working --- scrabble.js | 16 ++++++++++++---- spec/scrabble_spec.js | 34 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/scrabble.js b/scrabble.js index 2833c4b..19bbcc8 100644 --- a/scrabble.js +++ b/scrabble.js @@ -3,16 +3,24 @@ const letterPoints = { }; -let total = 0; - - const Scrabble = { score: function(word) { + + let total = 0; + + + word = word.toUpperCase(); + for(let i = 0; i < word.length; i++){ total += letterPoints[word[i]]; } // end for loop - return total; + + if(word.length >= 7){ + total += 50; + } + + return total; } //end function word diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 19abc80..efa066d 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -15,25 +15,25 @@ describe('score', 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 ('throws on bad characters', function() { + // expect(function () { + // Scrabble.score('char^'); + // }).toThrow(); + // }); - it ('does not allow words > 7 letters', function() { - expect(function() { Scrabble.score('abcdefgh'); }).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(); - }); + // it ('does not allow empty words', function() { + // expect(function() { Scrabble.score(''); }).toThrow(); + // }); }); // describe('highestScoreFrom', function() { From 25e303fd8e4b26c54f165d66512f4dcf040ca9d8 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 15:52:27 -0800 Subject: [PATCH 04/28] Finish scoring --- scrabble.js | 2 +- spec/scrabble_spec.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/scrabble.js b/scrabble.js index 19bbcc8..aaffc78 100644 --- a/scrabble.js +++ b/scrabble.js @@ -16,7 +16,7 @@ const Scrabble = { total += letterPoints[word[i]]; } // end for loop - if(word.length >= 7){ + if(word.length === 7){ total += 50; } diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index efa066d..a6c2b5e 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -21,12 +21,12 @@ 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 13a3b4b304beaf1ef05592fb6adec16227ddf565 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 16:04:07 -0800 Subject: [PATCH 05/28] Add exceptions for long/short words --- scrabble.js | 15 +++++++++++++-- spec/scrabble_spec.js | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index aaffc78..6391222 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,3 +1,9 @@ +function UserException(message) { + this.message = message; + this.name = 'UserException' +}; + + const letterPoints = { A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8, Y: 4, Z: 10 }; @@ -6,9 +12,14 @@ const letterPoints = { const Scrabble = { score: function(word) { + if(word.length > 7) { + throw new UserException("Word is Too Long"); + } else if (word.length ==- 0) { + throw new UserException("You must enter a word to be scored" + ) + } - let total = 0; - + let total = 0; //set initial total word = word.toUpperCase(); diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index a6c2b5e..19abc80 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); @@ -27,13 +27,13 @@ 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(); - // }); + it ('does not allow empty words', function() { + expect(function() { Scrabble.score(''); }).toThrow(); + }); }); // describe('highestScoreFrom', function() { From 431a1ae0055b7fc284c9beda3aa170483ec420a3 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 16:18:18 -0800 Subject: [PATCH 06/28] Add exceptions to scoring method --- scrabble.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index 6391222..4f0e698 100644 --- a/scrabble.js +++ b/scrabble.js @@ -16,7 +16,9 @@ const Scrabble = { throw new UserException("Word is Too Long"); } else if (word.length ==- 0) { throw new UserException("You must enter a word to be scored" - ) + ); + } else if (word.match(/[^a-z]+/i)) { + throw new UserException("Word can only contain letters"); } let total = 0; //set initial total From c6843033a814b6bac25bc4b518c78ea18e0b1106 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 16:19:39 -0800 Subject: [PATCH 07/28] Tiny change --- scrabble.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 4f0e698..a1448be 100644 --- a/scrabble.js +++ b/scrabble.js @@ -3,7 +3,6 @@ function UserException(message) { this.name = 'UserException' }; - const letterPoints = { A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8, Y: 4, Z: 10 }; @@ -14,7 +13,7 @@ const Scrabble = { if(word.length > 7) { throw new UserException("Word is Too Long"); - } else if (word.length ==- 0) { + } else if (word.length === 0) { throw new UserException("You must enter a word to be scored" ); } else if (word.match(/[^a-z]+/i)) { @@ -23,7 +22,7 @@ const Scrabble = { let total = 0; //set initial total - word = word.toUpperCase(); + word = word.toUpperCase(); //set word to uppercase for(let i = 0; i < word.length; i++){ total += letterPoints[word[i]]; From d688e40b5e29f70b1e417a6e4586e524d57bf506 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 16:39:40 -0800 Subject: [PATCH 08/28] Add highest score from 1 word array --- scrabble.js | 17 +- spec/scrabble_spec.js | 380 +++++++++++++++++++++--------------------- 2 files changed, 201 insertions(+), 196 deletions(-) diff --git a/scrabble.js b/scrabble.js index a1448be..51a4657 100644 --- a/scrabble.js +++ b/scrabble.js @@ -7,16 +7,14 @@ const letterPoints = { A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, L: 1, M: 3, N: 1, O: 1, P: 3, Q: 10, R: 1, S: 1, T: 1, U: 1, V: 4, W: 4, X: 8, Y: 4, Z: 10 }; - const Scrabble = { score: function(word) { if(word.length > 7) { throw new UserException("Word is Too Long"); } else if (word.length === 0) { - throw new UserException("You must enter a word to be scored" - ); - } else if (word.match(/[^a-z]+/i)) { + throw new UserException("You must enter a word to be scored"); + } else if (word.match(/[^a-z]+/i)) { throw new UserException("Word can only contain letters"); } @@ -34,9 +32,16 @@ const Scrabble = { return total; - } //end function word + }, //end function word - // TODO: add the highestScoreFrom method + highestScoreFrom: function(words) { + if(words.length === 1) { + return words[0]; + } + } + // highestScoreFrom: function(words) { + // return words + // } }; //end const Scrabble diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 19abc80..c195a03 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -36,193 +36,193 @@ describe('score', function() { }); }); -// 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(); -// }); -// }); -// }); +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 fdd663ce52b2f7cb14078b9ccca6192ba81188f6 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 18:55:30 -0800 Subject: [PATCH 09/28] Empty array, nonArray and 1 item array work --- scrabble.js | 26 +++-- spec/scrabble_spec.js | 258 +++++++++++++++++++++--------------------- 2 files changed, 148 insertions(+), 136 deletions(-) diff --git a/scrabble.js b/scrabble.js index 51a4657..62d1438 100644 --- a/scrabble.js +++ b/scrabble.js @@ -16,7 +16,7 @@ const Scrabble = { throw new UserException("You must enter a word to be scored"); } else if (word.match(/[^a-z]+/i)) { throw new UserException("Word can only contain letters"); - } + } //exception tests let total = 0; //set initial total @@ -28,21 +28,33 @@ const Scrabble = { if(word.length === 7){ total += 50; - } + } //if length return total; }, //end function word highestScoreFrom: function(words) { + + if(words.length === 0) { + throw new UserException("Array is Empty"); + } else if (typeof words != 'object') { + throw new UserException("You must enter an array"); + } //exceptions + if(words.length === 1) { return words[0]; - } - } - // highestScoreFrom: function(words) { - // return words - // } + } else if(words.length >= 2) { + let max = Scrabble.score(words[0]); + words.forEach(function (word) { + if (Scrabble.score(word) > max){ + max = Scrabble.score(word); + } //if + }); //forEach + return max + } //else if + } }; //end const Scrabble Scrabble.Player = class { diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..d4fb126 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -97,132 +97,132 @@ describe('highestScoreFrom', function() { }); }); -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(); - }); - }); -}); +// 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 386c7cd08ada7e80601b1e5bbda6a17ba8604d85 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 19:08:13 -0800 Subject: [PATCH 10/28] Find highest score in an array with two or more words --- scrabble.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 62d1438..9563c62 100644 --- a/scrabble.js +++ b/scrabble.js @@ -45,11 +45,13 @@ const Scrabble = { if(words.length === 1) { return words[0]; } else if(words.length >= 2) { - let max = Scrabble.score(words[0]); + let max = words[0]; + + let maxarray = []; words.forEach(function (word) { - if (Scrabble.score(word) > max){ - max = Scrabble.score(word); + if (Scrabble.score(word) > Scrabble.score(max)) { + max = word; } //if }); //forEach return max From 680f5cb5119c0f801d17168fc15c1424a5adcc47 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 21:35:35 -0800 Subject: [PATCH 11/28] Try lots of array and object methods that don't work --- scrabble.js | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index 9563c62..e00ad87 100644 --- a/scrabble.js +++ b/scrabble.js @@ -45,17 +45,50 @@ const Scrabble = { if(words.length === 1) { return words[0]; } else if(words.length >= 2) { - let max = words[0]; - let maxarray = []; + let max = words[0]; words.forEach(function (word) { - if (Scrabble.score(word) > Scrabble.score(max)) { + if (word.length === 7){ max = word; } //if + return max }); //forEach - return max + //find score of each word, use as key with array of all words with that score; + + // let score_hash = {}; + + // words.forEach(function (word) { + // score_hash[Scrabble.score(word)] = [].push(word); + // if (Scrabble.score(word) > Scrabble.score(max) { + // max = word; + // }); + // return max + // }); + // words.forEach(function (word) { + // score_hash[word] = Scrabble.score(word); + // }) + let highestScoringWords = []; + + words.forEach(function (word) { + if (Scrabble.score(word) > Scrabble.score(max)) { + max = word; + } + + }); //forEach + return max + // words.forEach(function (word) { + // if (Scrabble.score(word) === max) { + // highestScoringWords.push(word); + // } //if + // return highestScoringWords; + // }); //forEach + + // if(highestScoringWords.length === 1) { + // return highestScoringWords[0]; + // } } //else if + // return highestScoringWords[0]; } }; //end const Scrabble From 1f83eaaa7e636a9e0c9cd9095edcb13e6309fa65 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 14 Nov 2017 22:24:55 -0800 Subject: [PATCH 12/28] Make high score array and choose 7 letter word --- scrabble.js | 118 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 77 insertions(+), 41 deletions(-) diff --git a/scrabble.js b/scrabble.js index e00ad87..cbaab95 100644 --- a/scrabble.js +++ b/scrabble.js @@ -46,50 +46,42 @@ const Scrabble = { return words[0]; } else if(words.length >= 2) { - let max = words[0]; + let highScore = Scrabble.score(words[0]); + let scoreHash = {}; + let highestScoringWord = ""; words.forEach(function (word) { - if (word.length === 7){ - max = word; - } //if - return max - }); //forEach - //find score of each word, use as key with array of all words with that score; - - // let score_hash = {}; - - // words.forEach(function (word) { - // score_hash[Scrabble.score(word)] = [].push(word); - // if (Scrabble.score(word) > Scrabble.score(max) { - // max = word; - // }); - // return max - // }); - // words.forEach(function (word) { - // score_hash[word] = Scrabble.score(word); - // }) - let highestScoringWords = []; - - words.forEach(function (word) { - if (Scrabble.score(word) > Scrabble.score(max)) { - max = word; - } - - }); //forEach - return max - // words.forEach(function (word) { - // if (Scrabble.score(word) === max) { - // highestScoringWords.push(word); - // } //if - // return highestScoringWords; - // }); //forEach - - // if(highestScoringWords.length === 1) { - // return highestScoringWords[0]; - // } + let wordScore = Scrabble.score(word); + if (scoreHash[wordScore] === undefined) { + scoreHash[wordScore] = [word]; + } else { + scoreHash[wordScore].push(word); + } + if (wordScore > highScore) { + highScore = wordScore; + } + }); + + let highestScoringWords = scoreHash[highScore]; + console.log("highest array: " + highestScoringWords); + + if (highestScoringWords.length === 1) { + highestScoringWord = highestScoringWords[0]; + // return highestScoringWords[0]; + } else { + highestScoringWords.forEach(function (word) { + if (word.length === 7) { + console.log("seven lettesr? " + word + word.length); + highestScoringWord = word; + } //if word length + }); //array iteration + } //else for array length + + return highestScoringWord; } //else if - // return highestScoringWords[0]; - } + + + } //end highestscorefrom }; //end const Scrabble Scrabble.Player = class { @@ -97,3 +89,47 @@ Scrabble.Player = class { }; module.exports = Scrabble; + +// highestScoreFrom: function(words) { +// +// if(words.length === 0) { +// throw new UserException("Array is Empty"); +// } else if (typeof words != 'object') { +// throw new UserException("You must enter an array"); +// } //exceptions +// +// if(words.length === 1) { +// return words[0]; +// } else if(words.length >= 2) { +// +// let max = words[0]; +// +// words.forEach(function (word) { +// if (word.length === 7){ +// max = word; +// } //if +// return max +// }); //forEach +// +// words.forEach(function (word) { +// if (Scrabble.score(word) > Scrabble.score(max)) { +// max = word; +// } //if +// +// }); //forEach +// return max + + + +// let score_hash = {}; + +// words.forEach(function (word) { +// score_hash[Scrabble.score(word)] = [].push(word); +// if (Scrabble.score(word) > Scrabble.score(max) { +// max = word; +// }); +// return max +// }); +// words.forEach(function (word) { +// score_hash[word] = Scrabble.score(word); +// }) From 0fff6e034910a0776d36712e44a7c035ec498a47 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 08:01:06 -0800 Subject: [PATCH 13/28] Almost fix tie breaker --- scrabble.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index cbaab95..d8fdc4b 100644 --- a/scrabble.js +++ b/scrabble.js @@ -50,6 +50,7 @@ const Scrabble = { let scoreHash = {}; let highestScoringWord = ""; + words.forEach(function (word) { let wordScore = Scrabble.score(word); if (scoreHash[wordScore] === undefined) { @@ -63,20 +64,29 @@ const Scrabble = { }); let highestScoringWords = scoreHash[highScore]; - console.log("highest array: " + highestScoringWords); + let minLength = highestScoringWords[0].length; if (highestScoringWords.length === 1) { highestScoringWord = highestScoringWords[0]; - // return highestScoringWords[0]; } else { highestScoringWords.forEach(function (word) { if (word.length === 7) { - console.log("seven lettesr? " + word + word.length); highestScoringWord = word; } //if word length - }); //array iteration + else { + highestScoringWords.forEach(function (word){ + if(word.length < minLength) { + minLength = word.length; + highestScoringWord = word; + } //inner if on min + }); //foreach with min + } //else line 76 + + } //else if start with 7 letters + }); //array iteration } //else for array length + return highestScoringWord; } //else if From 4ad21701bd576af1799322c511b684b4a82a4a8a Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 11:39:51 -0800 Subject: [PATCH 14/28] Debugging word length selection --- scrabble.js | 135 +++++++++++++++++++++++++++------------------------- 1 file changed, 70 insertions(+), 65 deletions(-) diff --git a/scrabble.js b/scrabble.js index d8fdc4b..6740699 100644 --- a/scrabble.js +++ b/scrabble.js @@ -45,53 +45,85 @@ const Scrabble = { if(words.length === 1) { return words[0]; } else if(words.length >= 2) { - + console.log("Im in the else if for when there are 2 or more words in array") let highScore = Scrabble.score(words[0]); + let scoreHash = {}; + let highestScoringWord = ""; + console.log('The high score is ') + console.log('highScore'); words.forEach(function (word) { let wordScore = Scrabble.score(word); + if (scoreHash[wordScore] === undefined) { + console.log('word is ' + word + ' score is ' + wordScore); + console.log('im about to make a hash'); scoreHash[wordScore] = [word]; - } else { + console.log('in the If, scoreHash is '); + console.log(scoreHash); + } //if hash undefined + else { scoreHash[wordScore].push(word); - } + console.log('in the Else, scorehash is '); + console.log(scoreHash); + } //else for hash creation if (wordScore > highScore) { + console.log("word is"); + console.log(word); + console.log("word score is "); + console.log(wordScore); + console.log("high score is "); + console.log(highScore); highScore = wordScore; - } - }); + } // if for finding high score + }); //end of first for.each + console.log('here is the hash: ') + console.log(scoreHash); let highestScoringWords = scoreHash[highScore]; let minLength = highestScoringWords[0].length; + console.log(`and here are the highestscoring words ${highestScoringWords}`); if (highestScoringWords.length === 1) { highestScoringWord = highestScoringWords[0]; - } else { + } //end check for just one winning word + //this esle will rune if there's more than one word + else { highestScoringWords.forEach(function (word) { + if (word.length === 7) { highestScoringWord = word; - } //if word length - else { - highestScoringWords.forEach(function (word){ - if(word.length < minLength) { - minLength = word.length; - highestScoringWord = word; - } //inner if on min - }); //foreach with min - } //else line 76 - - } //else if start with 7 letters - }); //array iteration - } //else for array length - + minLength = 0; + console.log('im in the if for 7 letter words'); + console.log(`highest scoring word is now: ${highestScoringWord}`); + console.log('minLength is now ' + minLength); + } //if word length === 7 + else { + console.log("im in the else for when there aren't seven letter words") + console.log(`minLength is ${minLength}`); + if(word.length < minLength) { + minLength = word.length; + highestScoringWord = word; + console.log(`In the else if for non-seven letter words checking word lengths`); + console.log(`highest scoring word is now: ${highestScoringWord}` ); + console.log(`minimum length is now ${minLength}` ); + } //else if for finding minimum + }// end else -- words that aren't seven letters + }); //end highest scoring word function + + } //end else for multiple high scoring words + console.log("Im about to return " + highestScoringWord); return highestScoringWord; - } //else if - + } //else if array has 2 or more words + console.log("really:"); + return highestScoringWord; } //end highestscorefrom + }; //end const Scrabble Scrabble.Player = class { @@ -99,47 +131,20 @@ Scrabble.Player = class { }; module.exports = Scrabble; +const loser = 'zzzzzz'; +const winner = 'iiiiddd'; + +// console.log("Comparing dog and pig"); +// console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); +// console.log("*********************"); +// console.log("starting iwth pig") +// console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); +// console.log(Scrabble.highestScoreFrom([loser, winner])); +// console.log("Comparing winner and loser"); +// console.log(Scrabble.highestScoreFrom([winner, loser])); +console.log("schekcing i, dog, cat"); +console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])); + + -// highestScoreFrom: function(words) { -// -// if(words.length === 0) { -// throw new UserException("Array is Empty"); -// } else if (typeof words != 'object') { -// throw new UserException("You must enter an array"); -// } //exceptions -// -// if(words.length === 1) { -// return words[0]; -// } else if(words.length >= 2) { -// -// let max = words[0]; -// -// words.forEach(function (word) { -// if (word.length === 7){ -// max = word; -// } //if -// return max -// }); //forEach -// -// words.forEach(function (word) { -// if (Scrabble.score(word) > Scrabble.score(max)) { -// max = word; -// } //if -// -// }); //forEach -// return max - - - -// let score_hash = {}; - -// words.forEach(function (word) { -// score_hash[Scrabble.score(word)] = [].push(word); -// if (Scrabble.score(word) > Scrabble.score(max) { -// max = word; -// }); -// return max -// }); -// words.forEach(function (word) { -// score_hash[word] = Scrabble.score(word); -// }) +/// From 6c929abb7fc511c54ac50656b7e2f48a1991c148 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 11:49:59 -0800 Subject: [PATCH 15/28] Pass all tests for wave 1 --- scrabble.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scrabble.js b/scrabble.js index 6740699..0c61c3f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -52,8 +52,8 @@ const Scrabble = { let highestScoringWord = ""; - console.log('The high score is ') - console.log('highScore'); + console.log(`The high score is ${highScore}`); + words.forEach(function (word) { let wordScore = Scrabble.score(word); @@ -90,8 +90,9 @@ const Scrabble = { if (highestScoringWords.length === 1) { highestScoringWord = highestScoringWords[0]; } //end check for just one winning word - //this esle will rune if there's more than one word + //this esle will rune if there's more than one word ["dog", "cat"] else { + highestScoringWord = highestScoringWords[0]; highestScoringWords.forEach(function (word) { if (word.length === 7) { @@ -104,6 +105,7 @@ const Scrabble = { else { console.log("im in the else for when there aren't seven letter words") + console.log(`highest scoring word is ${highestScoringWord}`); console.log(`minLength is ${minLength}`); if(word.length < minLength) { minLength = word.length; @@ -112,6 +114,7 @@ const Scrabble = { console.log(`highest scoring word is now: ${highestScoringWord}` ); console.log(`minimum length is now ${minLength}` ); } //else if for finding minimum + }// end else -- words that aren't seven letters }); //end highest scoring word function @@ -131,19 +134,19 @@ Scrabble.Player = class { }; module.exports = Scrabble; -const loser = 'zzzzzz'; -const winner = 'iiiiddd'; - -// console.log("Comparing dog and pig"); -// console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); -// console.log("*********************"); -// console.log("starting iwth pig") -// console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); -// console.log(Scrabble.highestScoreFrom([loser, winner])); -// console.log("Comparing winner and loser"); -// console.log(Scrabble.highestScoreFrom([winner, loser])); -console.log("schekcing i, dog, cat"); -console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cat'])); +// const loser = 'zzzzzz'; +// const winner = 'iiiiddd'; +// +// // console.log("Comparing dog and pig"); +// // console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); +// // console.log("*********************"); +// // console.log("starting iwth pig") +// // console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); +// // console.log(Scrabble.highestScoreFrom([loser, winner])); +// // console.log("Comparing winner and loser"); +// // console.log(Scrabble.highestScoreFrom([winner, loser])); +// console.log("schekcing i, dog, cat"); +// console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cd', 'cat'])); From 64afd58636834795293356865d5d22067067eec5 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 11:52:14 -0800 Subject: [PATCH 16/28] Remove lots of console logs --- scrabble.js | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0c61c3f..69f1f5f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -45,52 +45,33 @@ const Scrabble = { if(words.length === 1) { return words[0]; } else if(words.length >= 2) { - console.log("Im in the else if for when there are 2 or more words in array") let highScore = Scrabble.score(words[0]); let scoreHash = {}; let highestScoringWord = ""; - console.log(`The high score is ${highScore}`); - - words.forEach(function (word) { let wordScore = Scrabble.score(word); if (scoreHash[wordScore] === undefined) { - console.log('word is ' + word + ' score is ' + wordScore); - console.log('im about to make a hash'); scoreHash[wordScore] = [word]; - console.log('in the If, scoreHash is '); - console.log(scoreHash); } //if hash undefined else { scoreHash[wordScore].push(word); - console.log('in the Else, scorehash is '); - console.log(scoreHash); } //else for hash creation if (wordScore > highScore) { - console.log("word is"); - console.log(word); - console.log("word score is "); - console.log(wordScore); - console.log("high score is "); - console.log(highScore); highScore = wordScore; } // if for finding high score }); //end of first for.each - console.log('here is the hash: ') - console.log(scoreHash); + let highestScoringWords = scoreHash[highScore]; let minLength = highestScoringWords[0].length; - console.log(`and here are the highestscoring words ${highestScoringWords}`); if (highestScoringWords.length === 1) { highestScoringWord = highestScoringWords[0]; } //end check for just one winning word - //this esle will rune if there's more than one word ["dog", "cat"] else { highestScoringWord = highestScoringWords[0]; highestScoringWords.forEach(function (word) { @@ -98,32 +79,18 @@ const Scrabble = { if (word.length === 7) { highestScoringWord = word; minLength = 0; - console.log('im in the if for 7 letter words'); - console.log(`highest scoring word is now: ${highestScoringWord}`); - console.log('minLength is now ' + minLength); } //if word length === 7 else { - console.log("im in the else for when there aren't seven letter words") - console.log(`highest scoring word is ${highestScoringWord}`); - console.log(`minLength is ${minLength}`); if(word.length < minLength) { minLength = word.length; highestScoringWord = word; - console.log(`In the else if for non-seven letter words checking word lengths`); - console.log(`highest scoring word is now: ${highestScoringWord}` ); - console.log(`minimum length is now ${minLength}` ); } //else if for finding minimum - }// end else -- words that aren't seven letters }); //end highest scoring word function - } //end else for multiple high scoring words - console.log("Im about to return " + highestScoringWord); return highestScoringWord; - } //else if array has 2 or more words - console.log("really:"); return highestScoringWord; } //end highestscorefrom From 1960214deb396f5cd82fbba8e8868a4dd01c9991 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 11:59:47 -0800 Subject: [PATCH 17/28] finish wave one --- scrabble.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/scrabble.js b/scrabble.js index 69f1f5f..945198a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -46,9 +46,7 @@ const Scrabble = { return words[0]; } else if(words.length >= 2) { let highScore = Scrabble.score(words[0]); - let scoreHash = {}; - let highestScoringWord = ""; words.forEach(function (word) { @@ -56,8 +54,7 @@ const Scrabble = { if (scoreHash[wordScore] === undefined) { scoreHash[wordScore] = [word]; - } //if hash undefined - else { + } else { scoreHash[wordScore].push(word); } //else for hash creation if (wordScore > highScore) { @@ -69,19 +66,17 @@ const Scrabble = { let highestScoringWords = scoreHash[highScore]; let minLength = highestScoringWords[0].length; + if (highestScoringWords.length === 1) { highestScoringWord = highestScoringWords[0]; - } //end check for just one winning word - else { + } else { highestScoringWord = highestScoringWords[0]; highestScoringWords.forEach(function (word) { if (word.length === 7) { highestScoringWord = word; minLength = 0; - } //if word length === 7 - - else { + } else { if(word.length < minLength) { minLength = word.length; highestScoringWord = word; @@ -91,9 +86,8 @@ const Scrabble = { } //end else for multiple high scoring words return highestScoringWord; } //else if array has 2 or more words - return highestScoringWord; + // return highestScoringWord; } //end highestscorefrom - }; //end const Scrabble Scrabble.Player = class { From a88890cb80f319904addc9c211781d8fa305bdd6 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 13:54:44 -0800 Subject: [PATCH 18/28] Update to throw error if no name --- scrabble.js | 35 ++++++++++++++++++++++++++++++++++- spec/scrabble_spec.js | 36 ++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/scrabble.js b/scrabble.js index 945198a..771d1e9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -51,7 +51,6 @@ const Scrabble = { words.forEach(function (word) { let wordScore = Scrabble.score(word); - if (scoreHash[wordScore] === undefined) { scoreHash[wordScore] = [word]; } else { @@ -91,10 +90,44 @@ const Scrabble = { }; //end const Scrabble Scrabble.Player = class { + constructor(name) { + if(name === undefined){ + throw new UserException("Must Include a Name"); + } + this.name = name; +} //constructor + +// if(name.length === 0) { +// throw new UserException("Must Include Name"); +// } else if (typeof words!= 'string') { +// throw new UserException("Name must be a string"); +// } +//exception tests + // TODO: implement the Player class }; module.exports = Scrabble; + + + +// it('Creates a new player', function() { +// let name = 'test name'; + // let player = new Scrabble.Player(); + // console.log(player); +// }); + +// it('Requires a name', function() { +// expect(function() { new Scrabble.Player(); }).toThrow(); +// }); + + + + + + + + // const loser = 'zzzzzz'; // const winner = 'iiiiddd'; // diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index d4fb126..3bc501c 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -97,23 +97,23 @@ describe('highestScoreFrom', function() { }); }); -// 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('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'; @@ -124,7 +124,7 @@ describe('highestScoreFrom', function() { // // 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'); From 9204291cc6b753f86c846e1317bf7fb329ab8523 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 14:43:16 -0800 Subject: [PATCH 19/28] Tiny change --- scrabble.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scrabble.js b/scrabble.js index 771d1e9..1deee0d 100644 --- a/scrabble.js +++ b/scrabble.js @@ -45,6 +45,7 @@ const Scrabble = { if(words.length === 1) { return words[0]; } else if(words.length >= 2) { + let highScore = Scrabble.score(words[0]); let scoreHash = {}; let highestScoringWord = ""; @@ -75,6 +76,7 @@ const Scrabble = { if (word.length === 7) { highestScoringWord = word; minLength = 0; + // return highestScoringWord; } else { if(word.length < minLength) { minLength = word.length; From a773b3c3e9029c5248f619a62e62bd4e764f54bd Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 15:51:13 -0800 Subject: [PATCH 20/28] fix most of the logic for highest scoring word --- scrabble.js | 116 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1deee0d..ae3b9c6 100644 --- a/scrabble.js +++ b/scrabble.js @@ -34,6 +34,28 @@ const Scrabble = { }, //end function word + tieBreak: function(a, b) { + console.log(`I'm in teh function and a is ${a} and b is ${b}`); + console.log(a.length); + if(a.length === 7){ + console.log(`${a} is seven letters`); + let winner = a; + return winner; + } else if(b.length === 7){ + let winner = b; + return winner; + } else if(a.length > b.length) { + console.log(`${b} is shorter than ${a}`); + let winner = b; + return winner; + } + // else { + // console.log(`b is ${b}`); + // let winner = b; + // return winner; + // } + }, //end tiebreak function + highestScoreFrom: function(words) { if(words.length === 0) { @@ -46,48 +68,24 @@ const Scrabble = { return words[0]; } else if(words.length >= 2) { - let highScore = Scrabble.score(words[0]); - let scoreHash = {}; let highestScoringWord = ""; + let highestScore = 0; words.forEach(function (word) { - let wordScore = Scrabble.score(word); - if (scoreHash[wordScore] === undefined) { - scoreHash[wordScore] = [word]; - } else { - scoreHash[wordScore].push(word); - } //else for hash creation - if (wordScore > highScore) { - highScore = wordScore; - } // if for finding high score - }); //end of first for.each - - - let highestScoringWords = scoreHash[highScore]; - let minLength = highestScoringWords[0].length; - - - if (highestScoringWords.length === 1) { - highestScoringWord = highestScoringWords[0]; - } else { - highestScoringWord = highestScoringWords[0]; - highestScoringWords.forEach(function (word) { - - if (word.length === 7) { - highestScoringWord = word; - minLength = 0; - // return highestScoringWord; - } else { - if(word.length < minLength) { - minLength = word.length; - highestScoringWord = word; - } //else if for finding minimum - }// end else -- words that aren't seven letters - }); //end highest scoring word function - } //end else for multiple high scoring words + + if(Scrabble.score(word) > highestScore){ + highestScoringWord = word; + highestScore = Scrabble.score(word); + }//if + else if (Scrabble.score(word) === highestScore) { + console.log("in the else if, sending words to tie break"); + console.log(`highestScoring is ${highestScoringWord} and it is being compared to ${word}`); + highestScoringWord = Scrabble.tieBreak(highestScoringWord, word); + console.log(`hsw is now ${highestScoringWord}`); + } //else if + });//end foreach function return highestScoringWord; } //else if array has 2 or more words - // return highestScoringWord; } //end highestscorefrom }; //end const Scrabble @@ -128,22 +126,40 @@ module.exports = Scrabble; +// +// // +const loser = 'zzzzzz'; +const winner = 'iiiiddd'; + +// console.log("Comparing dog and pig"); +// console.log("pig should win ") +// console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); +// console.log("*********************\n"); +// console.log("starting with pig, pig should win.") +// console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); +// +console.log("Comparing winner and loser"); +console.log("iiiiddd should win -- seven letters ") +console.log(Scrabble.highestScoreFrom([loser, winner])); +console.log(Scrabble.highestScoreFrom([winner, loser])); -// const loser = 'zzzzzz'; -// const winner = 'iiiiddd'; -// -// // console.log("Comparing dog and pig"); -// // console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); -// // console.log("*********************"); -// // console.log("starting iwth pig") -// // console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); -// // console.log(Scrabble.highestScoreFrom([loser, winner])); -// // console.log("Comparing winner and loser"); -// // console.log(Scrabble.highestScoreFrom([winner, loser])); -// console.log("schekcing i, dog, cat"); +// console.log("***************************************") +// console.log("schekcing i, dog, cat -- cd should win"); // console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cd', 'cat'])); - +// +// console.log("Comparing dog and cat, dog should win"); +// console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); +// console.log("***********************"); +// console.log("Comparing cat and dog, cat should win"); +// console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); +// console.log("***********************"); + // 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'); +// }); /// From 0323c05eed71955616e21c37b70b28dc14c52996 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 15:55:27 -0800 Subject: [PATCH 21/28] Fix Highest Score Logic good enough for now --- scrabble.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/scrabble.js b/scrabble.js index ae3b9c6..1c5fd47 100644 --- a/scrabble.js +++ b/scrabble.js @@ -42,12 +42,16 @@ const Scrabble = { let winner = a; return winner; } else if(b.length === 7){ + console.log(`${b} is seven letters`) let winner = b; return winner; } else if(a.length > b.length) { console.log(`${b} is shorter than ${a}`); let winner = b; return winner; + } else { + let winner = a; + return winner; } // else { // console.log(`b is ${b}`); @@ -128,8 +132,8 @@ module.exports = Scrabble; // // // -const loser = 'zzzzzz'; -const winner = 'iiiiddd'; +// const loser = 'zzzzzz'; +// const winner = 'iiiiddd'; // console.log("Comparing dog and pig"); // console.log("pig should win ") @@ -138,22 +142,22 @@ const winner = 'iiiiddd'; // console.log("starting with pig, pig should win.") // console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); // -console.log("Comparing winner and loser"); -console.log("iiiiddd should win -- seven letters ") -console.log(Scrabble.highestScoreFrom([loser, winner])); - -console.log(Scrabble.highestScoreFrom([winner, loser])); +// console.log("Comparing winner and loser"); +// console.log("iiiiddd should win -- seven letters ") +// console.log(Scrabble.highestScoreFrom([loser, winner])); +// +// console.log(Scrabble.highestScoreFrom([winner, loser])); // console.log("***************************************") // console.log("schekcing i, dog, cat -- cd should win"); // console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cd', 'cat'])); -// -// console.log("Comparing dog and cat, dog should win"); -// console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); -// console.log("***********************"); -// console.log("Comparing cat and dog, cat should win"); -// console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); -// console.log("***********************"); + +console.log("Comparing dog and cat, dog should win"); +console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); +console.log("***********************"); +console.log("Comparing cat and dog, cat should win"); +console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); +console.log("***********************"); // Test the functionality // expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); // expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); From 6efddea9b21f78a460916997e9a02a5b9c1da0f8 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 17:44:35 -0800 Subject: [PATCH 22/28] Add play method --- scrabble.js | 93 +++++++++--------- spec/scrabble_spec.js | 222 +++++++++++++++++++++--------------------- 2 files changed, 159 insertions(+), 156 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1c5fd47..0b77114 100644 --- a/scrabble.js +++ b/scrabble.js @@ -35,29 +35,19 @@ const Scrabble = { }, //end function word tieBreak: function(a, b) { - console.log(`I'm in teh function and a is ${a} and b is ${b}`); - console.log(a.length); if(a.length === 7){ - console.log(`${a} is seven letters`); let winner = a; return winner; } else if(b.length === 7){ - console.log(`${b} is seven letters`) let winner = b; return winner; } else if(a.length > b.length) { - console.log(`${b} is shorter than ${a}`); let winner = b; return winner; } else { let winner = a; return winner; } - // else { - // console.log(`b is ${b}`); - // let winner = b; - // return winner; - // } }, //end tiebreak function highestScoreFrom: function(words) { @@ -82,10 +72,7 @@ const Scrabble = { highestScore = Scrabble.score(word); }//if else if (Scrabble.score(word) === highestScore) { - console.log("in the else if, sending words to tie break"); - console.log(`highestScoring is ${highestScoringWord} and it is being compared to ${word}`); highestScoringWord = Scrabble.tieBreak(highestScoringWord, word); - console.log(`hsw is now ${highestScoringWord}`); } //else if });//end foreach function return highestScoringWord; @@ -97,40 +84,56 @@ Scrabble.Player = class { constructor(name) { if(name === undefined){ throw new UserException("Must Include a Name"); - } - this.name = name; -} //constructor - -// if(name.length === 0) { -// throw new UserException("Must Include Name"); -// } else if (typeof words!= 'string') { -// throw new UserException("Name must be a string"); -// } -//exception tests - - // TODO: implement the Player class -}; + } //exception + this.name = name; + this.plays = []; + console.log(this.plays); + console.log("constructor"); + } //constructor + + play(word) { + // let plays = ["fish"]; + // console.log(this.plays); + // console.log(`the word i'm playing is ${word}`); + this.plays.push(word); + }; //play function + +}; //player class module.exports = Scrabble; +// speak() { +// console.log(this.sound); +// } +// +// static createAnimals(sounds) { +// let animals = []; +// for (let sound of sounds) { +// let animal = new this(sound); +// animals.push(animal); +// } +// return animals; +// } +// } - -// it('Creates a new player', function() { -// let name = 'test name'; - // let player = new Scrabble.Player(); - // console.log(player); -// }); - -// it('Requires a name', function() { -// expect(function() { new Scrabble.Player(); }).toThrow(); -// }); +// - `name`: property which returns the value of the player's name +// - `plays`: property which returns an Array of the words played by the player +// - `play(word)`: method which adds the input word to the `plays` Array +// - Returns false if player has already won +// - `totalScore()`: method which sums up and returns the score of the players words +let word = 'dog'; +let player = new Scrabble.Player("Dave"); +console.log(player.plays.length); +player.play(word); +// expect(player.play(word)).toBeTruthy(); +console.log(player.plays.length); +console.log(player.plays[0]); -// // // // const loser = 'zzzzzz'; // const winner = 'iiiiddd'; @@ -151,14 +154,14 @@ module.exports = Scrabble; // console.log("***************************************") // console.log("schekcing i, dog, cat -- cd should win"); // console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cd', 'cat'])); - -console.log("Comparing dog and cat, dog should win"); -console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); -console.log("***********************"); -console.log("Comparing cat and dog, cat should win"); -console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); -console.log("***********************"); - // Test the functionality +// +// console.log("Comparing dog and cat, dog should win"); +// console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); +// console.log("***********************"); +// console.log("Comparing cat and dog, cat should win"); +// console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); +// console.log("***********************"); +// // Test the functionality // expect(Scrabble.highestScoreFrom(['dog', 'dog'])).toBe('dog'); // expect(Scrabble.highestScoreFrom(['dog', 'cat'])).toBe('dog'); // expect(Scrabble.highestScoreFrom(['cat', 'dog'])).toBe('cat'); diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index 3bc501c..c195a03 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -114,115 +114,115 @@ describe('Player', function() { }); }); -// 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'); + 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); }); -// -// 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(); -// }); -// }); -// }); + }); + + 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 0bfa094d80e6386f1d19bd96c94c62808cc0f6b2 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 18:09:47 -0800 Subject: [PATCH 23/28] fix truthy response in play function and add haswon function --- scrabble.js | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0b77114..1a34b48 100644 --- a/scrabble.js +++ b/scrabble.js @@ -87,17 +87,44 @@ Scrabble.Player = class { } //exception this.name = name; this.plays = []; - console.log(this.plays); - console.log("constructor"); } //constructor play(word) { - // let plays = ["fish"]; - // console.log(this.plays); - // console.log(`the word i'm playing is ${word}`); - this.plays.push(word); + + if(word.length > 7) { + throw new UserException("Word is Too Long"); + } else if (word.length === 0) { + throw new UserException("You must enter a word to be scored"); + } else if (word.match(/[^a-z]+/i)) { + throw new UserException("Word can only contain letters"); + } //exception tests + + + if((this.plays.length > 0) && (hasWon === false)) { + return false + } else { + this.plays.push(word); + return true; + } + + + }; //play function + totalScore() { + let totalScore = 0; + this.plays.forEach(function (word) { + total += Scrabble.score(word); + }); //for each + return totalScore; + } //ts method + + hasWon() { + if(totalScore >= 100) { + return true; + } + } + }; //player class module.exports = Scrabble; From 1fc0de837b5360f483203ccbfc39df7b3c825648 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 18:30:30 -0800 Subject: [PATCH 24/28] Work on Total Score and Has Won --- scrabble.js | 88 ++++++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 59 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1a34b48..5ae4e76 100644 --- a/scrabble.js +++ b/scrabble.js @@ -87,10 +87,28 @@ Scrabble.Player = class { } //exception this.name = name; this.plays = []; + // this.totalScore = 0; } //constructor - play(word) { + totalScore() { + console.log("Im in total score"); + let totalScore = 0; + this.plays.forEach(function (word) { + totalScore += Scrabble.score(word); + }); //for each + return totalScore; + } //ts method + hasWon() { + console.log("I'm in has Won."); + if(player.totalScore >= 100) { + console.log("total score is more than 100") + let hasWon = true; + return hasWon; + } + } + + play(word) { if(word.length > 7) { throw new UserException("Word is Too Long"); } else if (word.length === 0) { @@ -99,31 +117,16 @@ Scrabble.Player = class { throw new UserException("Word can only contain letters"); } //exception tests - - if((this.plays.length > 0) && (hasWon === false)) { + if((this.plays.length > 0) && (player.hasWon() === false)) { return false } else { this.plays.push(word); return true; } - - }; //play function - totalScore() { - let totalScore = 0; - this.plays.forEach(function (word) { - total += Scrabble.score(word); - }); //for each - return totalScore; - } //ts method - hasWon() { - if(totalScore >= 100) { - return true; - } - } }; //player class @@ -153,47 +156,14 @@ module.exports = Scrabble; let word = 'dog'; let player = new Scrabble.Player("Dave"); console.log(player.plays.length); -player.play(word); +player.play("zzzzzz"); +console.log(player.totalScore()); +player.play("zzzzzz"); +console.log(player.totalScore()); +player.play("shoes"); // expect(player.play(word)).toBeTruthy(); - +console.log("AFTER PLAYS"); console.log(player.plays.length); -console.log(player.plays[0]); - - - -// // -// const loser = 'zzzzzz'; -// const winner = 'iiiiddd'; - -// console.log("Comparing dog and pig"); -// console.log("pig should win ") -// console.log(Scrabble.highestScoreFrom(['dog', 'pig'])); -// console.log("*********************\n"); -// console.log("starting with pig, pig should win.") -// console.log(Scrabble.highestScoreFrom(['pig', 'dog'])); -// -// console.log("Comparing winner and loser"); -// console.log("iiiiddd should win -- seven letters ") -// console.log(Scrabble.highestScoreFrom([loser, winner])); -// -// console.log(Scrabble.highestScoreFrom([winner, loser])); - -// console.log("***************************************") -// console.log("schekcing i, dog, cat -- cd should win"); -// console.log(Scrabble.highestScoreFrom(['i', 'dog', 'cd', 'cat'])); -// -// console.log("Comparing dog and cat, dog should win"); -// console.log(Scrabble.highestScoreFrom(['dog', 'cat'])); -// console.log("***********************"); -// console.log("Comparing cat and dog, cat should win"); -// console.log(Scrabble.highestScoreFrom(['cat', 'dog'])); -// console.log("***********************"); -// // 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'); -// }); - - -/// +console.log(player.plays); +player.hasWon(); +console.log(player.totalScore()); From 6433654b6ff2a6d86aac0790fb0a4c5186c39361 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 19:01:54 -0800 Subject: [PATCH 25/28] Debugging haswon --- scrabble.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/scrabble.js b/scrabble.js index 5ae4e76..3ef2689 100644 --- a/scrabble.js +++ b/scrabble.js @@ -92,19 +92,24 @@ Scrabble.Player = class { totalScore() { console.log("Im in total score"); - let totalScore = 0; + let playerScore = 0; this.plays.forEach(function (word) { - totalScore += Scrabble.score(word); + playerScore += Scrabble.score(word); }); //for each - return totalScore; + return playerScore; } //ts method hasWon() { console.log("I'm in has Won."); - if(player.totalScore >= 100) { - console.log("total score is more than 100") - let hasWon = true; - return hasWon; + let score = player.totalScore(); + console.log(`score is ${score} and is that more than 100?`); + console.log(score); + console.log("*****************"); + if(score >= 100) { + console.log("total score is more than 100"); + return true; + } else { + return false; } } @@ -117,10 +122,18 @@ Scrabble.Player = class { throw new UserException("Word can only contain letters"); } //exception tests - if((this.plays.length > 0) && (player.hasWon() === false)) { - return false + console.log(`array length is ${this.plays.length}`); + console.log("__________________"); + console.log(`has won returns (true or false): `); + console.log(player.hasWon()); + console.log("________________"); +// if( (this.plays.length > 0) && (player.hasWon() != true)) + if(player.hasWon() != true) { + console.log("It shouldn't let you play this word"); + return false; } else { this.plays.push(word); + console.log("Im adding a word now"); return true; } @@ -156,14 +169,15 @@ module.exports = Scrabble; let word = 'dog'; let player = new Scrabble.Player("Dave"); console.log(player.plays.length); -player.play("zzzzzz"); +player.play("zzzzzzz"); console.log(player.totalScore()); -player.play("zzzzzz"); +player.play("qqqqqqq"); console.log(player.totalScore()); player.play("shoes"); // expect(player.play(word)).toBeTruthy(); console.log("AFTER PLAYS"); console.log(player.plays.length); console.log(player.plays); -player.hasWon(); +console.log("THis is showing if the player won"); +console.log(player.hasWon()); console.log(player.totalScore()); From b05627e2edb8218ccd163e4c702674b7f6bba622 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 15 Nov 2017 20:14:24 -0800 Subject: [PATCH 26/28] Functions are working, but won't pass all tests --- scrabble.js | 103 ++++++++++++++++++++++-------------------- spec/scrabble_spec.js | 4 +- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/scrabble.js b/scrabble.js index 3ef2689..1d32212 100644 --- a/scrabble.js +++ b/scrabble.js @@ -91,7 +91,6 @@ Scrabble.Player = class { } //constructor totalScore() { - console.log("Im in total score"); let playerScore = 0; this.plays.forEach(function (word) { playerScore += Scrabble.score(word); @@ -100,13 +99,8 @@ Scrabble.Player = class { } //ts method hasWon() { - console.log("I'm in has Won."); - let score = player.totalScore(); - console.log(`score is ${score} and is that more than 100?`); - console.log(score); - console.log("*****************"); - if(score >= 100) { - console.log("total score is more than 100"); + let score = player.totalScore(); //error line + if(score > 100) { return true; } else { return false; @@ -122,62 +116,75 @@ Scrabble.Player = class { throw new UserException("Word can only contain letters"); } //exception tests - console.log(`array length is ${this.plays.length}`); - console.log("__________________"); - console.log(`has won returns (true or false): `); - console.log(player.hasWon()); - console.log("________________"); -// if( (this.plays.length > 0) && (player.hasWon() != true)) - if(player.hasWon() != true) { - console.log("It shouldn't let you play this word"); + + if( (this.plays.length > 0) && (player.hasWon() === true)) { return false; } else { this.plays.push(word); - console.log("Im adding a word now"); return true; } }; //play function + highestScoringWord() { + return Scrabble.highestScoreFrom(this.plays); + } + + highestWordScore() { + + return Scrabble.score(Scrabble.highestScoreFrom(this.plays)); + } + }; //player class module.exports = Scrabble; -// speak() { -// console.log(this.sound); -// } -// -// static createAnimals(sounds) { -// let animals = []; -// for (let sound of sounds) { -// let animal = new this(sound); -// animals.push(animal); -// } -// return animals; -// } -// } - -// - `name`: property which returns the value of the player's name -// - `plays`: property which returns an Array of the words played by the player -// - `play(word)`: method which adds the input word to the `plays` Array -// - Returns false if player has already won -// - `totalScore()`: method which sums up and returns the score of the players words - - -let word = 'dog'; + + + + let player = new Scrabble.Player("Dave"); -console.log(player.plays.length); -player.play("zzzzzzz"); +console.log("Player begins game"); console.log(player.totalScore()); -player.play("qqqqqqq"); +player.play('dog'); console.log(player.totalScore()); -player.play("shoes"); -// expect(player.play(word)).toBeTruthy(); -console.log("AFTER PLAYS"); -console.log(player.plays.length); -console.log(player.plays); -console.log("THis is showing if the player won"); +player.play('cat'); +console.log(player.totalScore()); +player.play('goat'); +console.log(player.totalScore()); +console.log("has player won? "); console.log(player.hasWon()); +console.log("Plays zzzzzzz"); +player.play('zzzzzzz'); console.log(player.totalScore()); +console.log("has player won? "); +console.log(player.hasWon()); +console.log(player.highestScoringWord()); +console.log(player.highestWordScore()); + + + + + + + + + + + + +// console.log(player.plays.length); +// player.play("zzzzzzz"); +// console.log(player.totalScore()); +// player.play("qqqqqqq"); +// console.log(player.totalScore()); +// player.play("shoes"); +// // expect(player.play(word)).toBeTruthy(); +// console.log("AFTER PLAYS"); +// console.log(player.plays.length); +// console.log(player.plays); +// console.log("THis is showing if the player won"); +// console.log(player.hasWon()); +// console.log(player.totalScore()); diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..7188cf3 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -136,7 +136,7 @@ describe('Player', function() { expect(function() { player.play(44); }).toThrow(); expect(player.plays.length).toBe(0); }); - +//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 @@ -153,7 +153,7 @@ describe('Player', function() { let player = new Scrabble.Player('test player'); expect(player.totalScore()).toBe(0); }); - +//2 it ('Is updated by play', function() { let player = new Scrabble.Player('test player'); expect(player.totalScore()).toBe(0); From bd4586dcdcd9315380295590ae0b7d461f4bb403 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Thu, 16 Nov 2017 19:15:09 -0800 Subject: [PATCH 27/28] Finally passing all tests --- scrabble.js | 85 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1d32212..994ed86 100644 --- a/scrabble.js +++ b/scrabble.js @@ -80,7 +80,14 @@ const Scrabble = { } //end highestscorefrom }; //end const Scrabble -Scrabble.Player = class { + +// { +// constructor(title, author) { +// this.title = title; +// this._author = author; +// } + +Scrabble.Player = class { constructor(name) { if(name === undefined){ throw new UserException("Must Include a Name"); @@ -91,7 +98,9 @@ Scrabble.Player = class { } //constructor totalScore() { + console.log("Here in total Score"); let playerScore = 0; + // this. this.plays.forEach(function (word) { playerScore += Scrabble.score(word); }); //for each @@ -99,8 +108,10 @@ Scrabble.Player = class { } //ts method hasWon() { - let score = player.totalScore(); //error line - if(score > 100) { + console.log("here in has won"); + let score = this.totalScore(); //error line + // let score = player.totalScore(); //error line + if(score >= 100) { return true; } else { return false; @@ -116,10 +127,11 @@ Scrabble.Player = class { throw new UserException("Word can only contain letters"); } //exception tests - - if( (this.plays.length > 0) && (player.hasWon() === true)) { + if( (this.plays.length > 0) && (this.hasWon() === true)) { + // if( (this.plays.length > 0) && (player.hasWon() === true)) { return false; } else { + this.plays.push(word); return true; } @@ -144,27 +156,48 @@ module.exports = Scrabble; - -let player = new Scrabble.Player("Dave"); -console.log("Player begins game"); -console.log(player.totalScore()); -player.play('dog'); -console.log(player.totalScore()); -player.play('cat'); -console.log(player.totalScore()); -player.play('goat'); -console.log(player.totalScore()); -console.log("has player won? "); -console.log(player.hasWon()); -console.log("Plays zzzzzzz"); -player.play('zzzzzzz'); -console.log(player.totalScore()); -console.log("has player won? "); -console.log(player.hasWon()); -console.log(player.highestScoringWord()); -console.log(player.highestWordScore()); - - +// +// let player = new Scrabble.Player("Dave"); +// console.log("Player begins game"); +// console.log("Total score, start") +// console.log(player.totalScore()); +// player.play('dog'); +// console.log("total score after playing one word."); +// console.log(player.totalScore()); +// console.log("total score after playing next word"); +// player.play('cat'); +// console.log(player.totalScore()); +// player.play('goat'); +// console.log(player.totalScore()); +// // console.log("has player won? "); +// console.log(player.hasWon()); +// console.log("Plays zzzzzzz"); +// player.play('zzzzzzz'); +// console.log(player.totalScore()); +// console.log("has player won? "); +// console.log(player.hasWon()); +// console.log(player.highestScoringWord()); +// console.log(player.highestWordScore()); +// console.log("*****************"); +// let player2 = new Scrabble.Player("Vee"); +// console.log("Player begins game"); +// console.log(player2.totalScore()); +// player2.play('dog'); +// console.log(player2.totalScore()); +// player2.play('cat'); +// console.log(player2.totalScore()); +// player.play('goat'); +// console.log(player2.totalScore()); +// console.log("has player won? "); +// console.log(player2.hasWon()); +// console.log("Plays zzzzzzz"); +// player2.play('zzzzzzz'); +// console.log(player2.totalScore()); +// console.log("has player won? "); +// console.log(player2.hasWon()); +// console.log(player2.highestScoringWord()); +// console.log(player2.highestWordScore()); +// From f8e5568eb3aab2601df642904534c56391542c89 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Fri, 17 Nov 2017 07:35:57 -0800 Subject: [PATCH 28/28] final --- scrabble.js | 144 ++++++++++------------------------------------------ 1 file changed, 27 insertions(+), 117 deletions(-) diff --git a/scrabble.js b/scrabble.js index 994ed86..4eb94ef 100644 --- a/scrabble.js +++ b/scrabble.js @@ -16,39 +16,35 @@ const Scrabble = { throw new UserException("You must enter a word to be scored"); } else if (word.match(/[^a-z]+/i)) { throw new UserException("Word can only contain letters"); - } //exception tests + } - let total = 0; //set initial total + let total = 0; - word = word.toUpperCase(); //set word to uppercase + word = word.toUpperCase(); for(let i = 0; i < word.length; i++){ total += letterPoints[word[i]]; - } // end for loop + } if(word.length === 7){ total += 50; - } //if length + } return total; - }, //end function word + }, tieBreak: function(a, b) { if(a.length === 7){ - let winner = a; - return winner; + return a; } else if(b.length === 7){ - let winner = b; - return winner; + return b; } else if(a.length > b.length) { - let winner = b; - return winner; + return b; } else { - let winner = a; - return winner; + return a; } - }, //end tiebreak function + }, highestScoreFrom: function(words) { @@ -56,7 +52,7 @@ const Scrabble = { throw new UserException("Array is Empty"); } else if (typeof words != 'object') { throw new UserException("You must enter an array"); - } //exceptions + } if(words.length === 1) { return words[0]; @@ -70,47 +66,36 @@ const Scrabble = { if(Scrabble.score(word) > highestScore){ highestScoringWord = word; highestScore = Scrabble.score(word); - }//if + } else if (Scrabble.score(word) === highestScore) { highestScoringWord = Scrabble.tieBreak(highestScoringWord, word); - } //else if - });//end foreach function + } + }); return highestScoringWord; - } //else if array has 2 or more words - } //end highestscorefrom -}; //end const Scrabble - + } + } +}; -// { -// constructor(title, author) { -// this.title = title; -// this._author = author; -// } Scrabble.Player = class { constructor(name) { if(name === undefined){ throw new UserException("Must Include a Name"); - } //exception + } this.name = name; this.plays = []; - // this.totalScore = 0; - } //constructor + } totalScore() { - console.log("Here in total Score"); let playerScore = 0; - // this. this.plays.forEach(function (word) { playerScore += Scrabble.score(word); - }); //for each + }); return playerScore; - } //ts method + } hasWon() { - console.log("here in has won"); - let score = this.totalScore(); //error line - // let score = player.totalScore(); //error line + let score = this.totalScore(); if(score >= 100) { return true; } else { @@ -122,102 +107,27 @@ Scrabble.Player = class { if(word.length > 7) { throw new UserException("Word is Too Long"); } else if (word.length === 0) { - throw new UserException("You must enter a word to be scored"); + throw new UserException("You must enter a word"); } else if (word.match(/[^a-z]+/i)) { throw new UserException("Word can only contain letters"); - } //exception tests + } if( (this.plays.length > 0) && (this.hasWon() === true)) { - // if( (this.plays.length > 0) && (player.hasWon() === true)) { return false; } else { - this.plays.push(word); return true; } - - }; //play function + }; highestScoringWord() { return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { - return Scrabble.score(Scrabble.highestScoreFrom(this.plays)); } - - -}; //player class +}; module.exports = Scrabble; - - - - -// -// let player = new Scrabble.Player("Dave"); -// console.log("Player begins game"); -// console.log("Total score, start") -// console.log(player.totalScore()); -// player.play('dog'); -// console.log("total score after playing one word."); -// console.log(player.totalScore()); -// console.log("total score after playing next word"); -// player.play('cat'); -// console.log(player.totalScore()); -// player.play('goat'); -// console.log(player.totalScore()); -// // console.log("has player won? "); -// console.log(player.hasWon()); -// console.log("Plays zzzzzzz"); -// player.play('zzzzzzz'); -// console.log(player.totalScore()); -// console.log("has player won? "); -// console.log(player.hasWon()); -// console.log(player.highestScoringWord()); -// console.log(player.highestWordScore()); -// console.log("*****************"); -// let player2 = new Scrabble.Player("Vee"); -// console.log("Player begins game"); -// console.log(player2.totalScore()); -// player2.play('dog'); -// console.log(player2.totalScore()); -// player2.play('cat'); -// console.log(player2.totalScore()); -// player.play('goat'); -// console.log(player2.totalScore()); -// console.log("has player won? "); -// console.log(player2.hasWon()); -// console.log("Plays zzzzzzz"); -// player2.play('zzzzzzz'); -// console.log(player2.totalScore()); -// console.log("has player won? "); -// console.log(player2.hasWon()); -// console.log(player2.highestScoringWord()); -// console.log(player2.highestWordScore()); -// - - - - - - - - - - -// console.log(player.plays.length); -// player.play("zzzzzzz"); -// console.log(player.totalScore()); -// player.play("qqqqqqq"); -// console.log(player.totalScore()); -// player.play("shoes"); -// // expect(player.play(word)).toBeTruthy(); -// console.log("AFTER PLAYS"); -// console.log(player.plays.length); -// console.log(player.plays); -// console.log("THis is showing if the player won"); -// console.log(player.hasWon()); -// console.log(player.totalScore());