From 42fa46c10d1641b6e3a5758b1ec4aacee3869b5a Mon Sep 17 00:00:00 2001 From: Kate Evans-Spitzer Date: Tue, 14 Nov 2017 14:32:37 -0800 Subject: [PATCH 1/5] initialize npm --- 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 1ea4b1c5583b4b8ae5b30ea084a76afe696139cf Mon Sep 17 00:00:00 2001 From: Kate Evans-Spitzer Date: Tue, 14 Nov 2017 15:23:24 -0800 Subject: [PATCH 2/5] implement Scrabble#score --- scrabble.js | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7a1f161..8475e96 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,46 @@ const Scrabble = { - score: function(word) { - // TODO: implement score - } + VALUES: { + 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 + }, + wordCheck: /\W|_|\d/, + + score: function score(word) { + let wordScore = 0; + + if (Scrabble.wordCheck.test(word)) { + throw new Error('Input contains invalid characters'); + } + word = word.toUpperCase(); + + if (word.length === 7) { + wordScore += 50; + } else if (word.length > 7) { + throw new Error('Input is too long - cannot exceed 7 letters'); + } else if (word.length === 0) { + throw new Error('Input cannot be empty'); + } + for (let letter of word) { + wordScore += Scrabble.VALUES[letter]; + } + return wordScore; + }, + + highestScoreFrom: function highestScoreFrom(words) { + let highestWord = words[0]; + let bestScore = 0; + let currentScore = 0; + for (let word of words) { + currentScore = Scrabble.score(word); + if (currentScore > bestScore) { + highestWord = word; + bestScore = currentScore; + } + } + return highestWord; + } // TODO: add the highestScoreFrom method }; @@ -12,3 +50,5 @@ Scrabble.Player = class { }; module.exports = Scrabble; + +console.log(Scrabble.score('dog')); From b87cc39b924a83796912a50911a25d23195222fc Mon Sep 17 00:00:00 2001 From: Kate Evans-Spitzer Date: Tue, 14 Nov 2017 15:40:10 -0800 Subject: [PATCH 3/5] implement Scrabble#highestWordFrom --- scrabble.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scrabble.js b/scrabble.js index 8475e96..b498b9a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -29,6 +29,12 @@ const Scrabble = { }, highestScoreFrom: function highestScoreFrom(words) { + if (words.length === 0) { + throw new Error('Input cannot be empty'); + } else if (words.length === 1) { + return words[0]; + } + let highestWord = words[0]; let bestScore = 0; let currentScore = 0; @@ -37,6 +43,13 @@ const Scrabble = { if (currentScore > bestScore) { highestWord = word; bestScore = currentScore; + } else if (currentScore === bestScore) { + if (highestWord.length != 7) { + if (word.length === 7 || word.length < highestWord.length) { + highestWord = word; + bestScore = currentScore; + } + } } } return highestWord; From 1563cbc515f54c3fe7b5224665744711e67a9e65 Mon Sep 17 00:00:00 2001 From: Kate Evans-Spitzer Date: Tue, 14 Nov 2017 16:02:29 -0800 Subject: [PATCH 4/5] implement Scrabble.Player#play, #totalScore --- scrabble.js | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/scrabble.js b/scrabble.js index b498b9a..1b5b82b 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,3 +1,7 @@ +const requiredParams = function requiredParams(param) { + throw new Error(`Required param ${param}`) +}; + const Scrabble = { VALUES: { A: 1, B: 3, C: 3, D: 2, E: 1, F: 4, G: 2, H: 4, I: 1, J: 8, K: 5, @@ -12,16 +16,16 @@ const Scrabble = { if (Scrabble.wordCheck.test(word)) { throw new Error('Input contains invalid characters'); } - word = word.toUpperCase(); - if (word.length === 7) { + if (!word || word.length === 0) { + throw new Error('Input cannot be empty'); + } else if (word.length === 7) { wordScore += 50; } else if (word.length > 7) { throw new Error('Input is too long - cannot exceed 7 letters'); - } else if (word.length === 0) { - throw new Error('Input cannot be empty'); } + word = word.toUpperCase(); for (let letter of word) { wordScore += Scrabble.VALUES[letter]; } @@ -54,14 +58,28 @@ const Scrabble = { } return highestWord; } - // TODO: add the highestScoreFrom method - }; Scrabble.Player = class { - // TODO: implement the Player class + constructor(name = requiredParams('name')){ + this.name = name; + this.plays = []; + this._totalScore = 0; + } + + totalScore() { + return this._totalScore; + } + + play(word) { + let score = Scrabble.score(word); + if (word) { + this.plays.push(word); + this._totalScore += score; + return this.plays; + } + return false; + } }; module.exports = Scrabble; - -console.log(Scrabble.score('dog')); From adc98591eceb94918b70d390609cc62005984f21 Mon Sep 17 00:00:00 2001 From: Kate Evans-Spitzer Date: Tue, 14 Nov 2017 16:34:28 -0800 Subject: [PATCH 5/5] implement Scrabble.Player#highestScoringWord and #highestWordScore --- scrabble.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index 1b5b82b..b054e61 100644 --- a/scrabble.js +++ b/scrabble.js @@ -65,21 +65,55 @@ Scrabble.Player = class { this.name = name; this.plays = []; this._totalScore = 0; + this._hasWon = false; + this._highestScoringWord; + this._highestWordScore = 0; } totalScore() { return this._totalScore; } + highestScoringWord() { + if (this._highestScoringWord) { + return this._highestScoringWord; + } else { + throw new Error(`Player ${this.name} has not played any words. `); + } + } + + highestWordScore() { + if (this._highestWordScore) { + return this._highestWordScore; + } else { + throw new Error(`Player ${this.name} has not played any words. `); + } + } + + hasWon() { + return this._hasWon; + } + play(word) { let score = Scrabble.score(word); - if (word) { + if (word && !this._hasWon) { this.plays.push(word); - this._totalScore += score; + this.addScore(score); + if (score > this._highestWordScore) { + this._highestScoringWord = word; + this._highestWordScore = score; + } return this.plays; } return false; } + + addScore(score) { + this._totalScore += score; + if (this._totalScore >= 100) { + this._hasWon = true; + } + } }; module.exports = Scrabble;