From d1b67f2508e4b7ad4a376d4f877c0c0985fdd1ca Mon Sep 17 00:00:00 2001 From: Tanisha Date: Thu, 16 Nov 2017 19:07:27 -0800 Subject: [PATCH 1/5] update score, highestScoreFrom, and player class --- package-lock.json | 123 ++++++++++++++++++++++++++++++++++++++++++++++ scrabble.js | 122 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 242 insertions(+), 3 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..80043d7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,123 @@ +{ + "name": "js-scrabble", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "add-matchers": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/add-matchers/-/add-matchers-0.5.0.tgz", + "integrity": "sha1-UCGQ5HUM1XIWGDkyaLYaFXNm52U=" + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", + "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "requires": { + "balanced-match": "1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "eslint-plugin-jasmine": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-jasmine/-/eslint-plugin-jasmine-2.9.1.tgz", + "integrity": "sha1-IuGaWfFvOl9kOgSroEQ40OMEcDA=" + }, + "exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=" + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "jasmine": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-2.8.0.tgz", + "integrity": "sha1-awicChFXax8W3xG4AUbZHU6Lij4=", + "requires": { + "exit": "0.1.2", + "glob": "7.1.2", + "jasmine-core": "2.8.0" + } + }, + "jasmine-core": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-2.8.0.tgz", + "integrity": "sha1-vMl5rh+f0FcB5F5S5l06XWPxok4=" + }, + "jasmine-expect": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/jasmine-expect/-/jasmine-expect-3.8.1.tgz", + "integrity": "sha512-klARdR5AVX9nZhHhYDlbDYgxgi6kl9DGS0vguhaioKoSwr8HL1uOQ7FFUBASq/sqBL/s2nkh/1efqZ2ugcknFA==", + "requires": { + "add-matchers": "0.5.0" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "1.1.8" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + } + } +} diff --git a/scrabble.js b/scrabble.js index 7a1f161..74011b6 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,14 +1,130 @@ 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, + 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, + }, score: function(word) { - // TODO: implement score - } + // TODO: implement score //score(word): returns the total score value for the given word. The word is input as a string (case insensitive). The chart below shows the point value for a given letter + + let totalScore = 0; + let regex = /^[a-zA-Z]+$/; + + word = word.toUpperCase(); + + if (regex.test(word) != true) { + throw 'Error'; + } + // score adds 50 points for a 7-letter word + if (word.length == 7) { + totalScore += 50; + } else if (word.length > 7) { + throw 'Error'; + } + // calculates score value for the given word + for (let letter of word) { + totalScore += this.values[letter]; + } + return totalScore; + }, + + // TODO: add the highestScoreFrom method // returns the word in the array with the highest score + highestScoreFrom: function(arrayOfWords) { + let scores = []; + if (arrayOfWords.length == 0) { + throw 'Error'; + } + + for (let word of arrayOfWords) { + wordScore = this.score(word); + // scores.word = wordScore; + scores.push(wordScore); + } + + highestScoreIndex = scores.indexOf(Math.max(...scores)); - // TODO: add the highestScoreFrom method + highestScore = Math.max(...scores); + + let tieIndices = []; + for (let i = 0; i < scores.length; i++) { + if (scores[i] == highestScore) { + tieIndices.push(i); + } + } + let tieBreaker = ''; + for (let i = 0; i < tieIndices.length; i++) { + if (arrayOfWords[tieIndices[i]].length == 7 ) { + return arrayOfWords[tieIndices[i]]; + } + + } + // return shortest string + // var arr = ['cats', 'giants', 'daughters', 'ice']; + // var min = Math.min(...arr.map(({ length }) => length)); + // console.log(min); + + // let tieIndices = scores.filter(function(score){ + // return score == scores[highestScoreIndex]; + // }); + // console.log(`ties:${tieIndices}`); + if (arrayOfWords.length < 2) { + return arrayOfWords[0]; + // } else if () { + + } else { + return arrayOfWords[highestScoreIndex]; + } + } }; Scrabble.Player = class { // TODO: implement the Player class + constructor(name) { + this.name = name; + this.plays = []; + } + + // if (this.name == null) { + // throw 'Error'; + // } + + play(word) { + this.plays.push(word); + // return false if hasWon == true + } + + totalScore() { + // sums up and returns the score of the players words + let total = 0; + for (let i = 0; i < this.plays.length; i++) { + // score each word + return total += score(this.plays[i]); + // return total score + } + } + + hasWon() { + // returns `true` if the player has over 100 points, otherwise returns `false` + if (totalScore() >= 100) { + return true; + } else { + return false; + } + } + + highestScoringWord() { + // method which returns the highest scoring word the user has played + highestScoreFrom(plays); + } + + highestWordScore() { + // returns the `highestScoringWord` score + score(highestScoreFrom(plays)); + } + }; module.exports = Scrabble; + +Scrabble.highestScoreFrom(['dog', 'pig','zzzzzz','iiiiddd']); From 570cef6f28e62eed3f7b4b123fd776e967468d13 Mon Sep 17 00:00:00 2001 From: Tanisha Date: Fri, 17 Nov 2017 00:11:48 -0800 Subject: [PATCH 2/5] update totalscore --- scrabble.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/scrabble.js b/scrabble.js index 74011b6..9d1cdbd 100644 --- a/scrabble.js +++ b/scrabble.js @@ -83,46 +83,48 @@ Scrabble.Player = class { constructor(name) { this.name = name; this.plays = []; + if (this.name == null) { + throw 'Error'; + } } - // if (this.name == null) { - // throw 'Error'; - // } - play(word) { - this.plays.push(word); - // return false if hasWon == true + if (this.hasWon() === true) { + return false; + } + if (typeof word !== 'string') { + throw 'Error'; + } else { + return this.plays.push(word); + } } totalScore() { - // sums up and returns the score of the players words let total = 0; - for (let i = 0; i < this.plays.length; i++) { - // score each word - return total += score(this.plays[i]); - // return total score + for (let word of this.plays) { + total += Scrabble.score(word); } + return total; } hasWon() { // returns `true` if the player has over 100 points, otherwise returns `false` - if (totalScore() >= 100) { + if (this.totalScore() >= 100) { return true; } else { - return false; + return false; } } highestScoringWord() { // method which returns the highest scoring word the user has played - highestScoreFrom(plays); + return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { // returns the `highestScoringWord` score - score(highestScoreFrom(plays)); + return Scrabble.score(Scrabble.highestScoreFrom(this.plays)); } - }; module.exports = Scrabble; From ca3052f1379613a35b7633708b4449f00274a7ba Mon Sep 17 00:00:00 2001 From: Tanisha Date: Fri, 17 Nov 2017 00:27:01 -0800 Subject: [PATCH 3/5] add shortestWord method --- scrabble.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index 9d1cdbd..9c78bb2 100644 --- a/scrabble.js +++ b/scrabble.js @@ -28,8 +28,8 @@ const Scrabble = { return totalScore; }, - // TODO: add the highestScoreFrom method // returns the word in the array with the highest score highestScoreFrom: function(arrayOfWords) { + // returns the word in the array with the highest score let scores = []; if (arrayOfWords.length == 0) { throw 'Error'; @@ -51,13 +51,25 @@ const Scrabble = { tieIndices.push(i); } } + + let shortestWord = function shortestWord(array) { + return array.reduce(function(shortestSoFar, current) { + if (current.length < shortestSoFar.length) { + return current; + } else { + return shortestSoFar; + } + }); + } + let tieBreaker = ''; for (let i = 0; i < tieIndices.length; i++) { if (arrayOfWords[tieIndices[i]].length == 7 ) { return arrayOfWords[tieIndices[i]]; } - } + + // return shortest string // var arr = ['cats', 'giants', 'daughters', 'ice']; // var min = Math.min(...arr.map(({ length }) => length)); From a7d109fb69baff29679773c4134588356d6c4713 Mon Sep 17 00:00:00 2001 From: Tanisha Date: Fri, 17 Nov 2017 00:33:48 -0800 Subject: [PATCH 4/5] add coments --- scrabble.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index 9c78bb2..0a99744 100644 --- a/scrabble.js +++ b/scrabble.js @@ -31,27 +31,29 @@ const Scrabble = { highestScoreFrom: function(arrayOfWords) { // returns the word in the array with the highest score let scores = []; + // throw error if arrayOfWords is empty if (arrayOfWords.length == 0) { throw 'Error'; } - + // score each word and push onto scores array for (let word of arrayOfWords) { wordScore = this.score(word); - // scores.word = wordScore; scores.push(wordScore); } - + // find index of highest score highestScoreIndex = scores.indexOf(Math.max(...scores)); - + // find highest score highestScore = Math.max(...scores); let tieIndices = []; + // loop through scores to find ties and capture indices of ties for (let i = 0; i < scores.length; i++) { if (scores[i] == highestScore) { tieIndices.push(i); } } + // find shortest word let shortestWord = function shortestWord(array) { return array.reduce(function(shortestSoFar, current) { if (current.length < shortestSoFar.length) { @@ -63,6 +65,8 @@ const Scrabble = { } let tieBreaker = ''; + // if tie word if seven, it wins + // if neither is seven, shortestWord wins for (let i = 0; i < tieIndices.length; i++) { if (arrayOfWords[tieIndices[i]].length == 7 ) { return arrayOfWords[tieIndices[i]]; From 8da51a2dff9fe3fe627a6e855b44d23b7aa682b8 Mon Sep 17 00:00:00 2001 From: Tanisha Date: Fri, 17 Nov 2017 00:53:45 -0800 Subject: [PATCH 5/5] update comments --- scrabble.js | 75 +++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0a99744..0f5ee98 100644 --- a/scrabble.js +++ b/scrabble.js @@ -53,45 +53,52 @@ const Scrabble = { } } - // find shortest word - let shortestWord = function shortestWord(array) { - return array.reduce(function(shortestSoFar, current) { - if (current.length < shortestSoFar.length) { - return current; - } else { - return shortestSoFar; - } - }); - } - let tieBreaker = ''; - // if tie word if seven, it wins - // if neither is seven, shortestWord wins + // TODO: lines 58 - 87 do not work right now. I would to use a tieBreaker function to hold the tie rules and call it when i am scoring + // // find shortest word + // let shortestWord = function shortestWord(array) { + // return array.reduce(function(shortestSoFar, current) { + // if (current.length < shortestSoFar.length) { + // return current; + // } else { + // return shortestSoFar; + // } + // }); + // } + // + // let tieBreaker = function tieBreaker() { + // // if tie word is seven, it wins + // // if neither is seven, shortestWord wins + // for (let i = 0; i < tieIndices.length; i++) { + // if (arrayOfWords[tieIndices[i]].length == 7 ) { + // return arrayOfWords[tieIndices[i]]; + // } + // } + // return shortestWord; + // } + // + // if (arrayOfWords.length < 2) { + // return arrayOfWords[0]; + // } else if (tieIndices >= 2) { + // return tieBreaker(); + // } else { + // return arrayOfWords[highestScoreIndex]; + // } + // } for (let i = 0; i < tieIndices.length; i++) { if (arrayOfWords[tieIndices[i]].length == 7 ) { return arrayOfWords[tieIndices[i]]; } } - - // return shortest string - // var arr = ['cats', 'giants', 'daughters', 'ice']; - // var min = Math.min(...arr.map(({ length }) => length)); - // console.log(min); - - // let tieIndices = scores.filter(function(score){ - // return score == scores[highestScoreIndex]; - // }); - // console.log(`ties:${tieIndices}`); if (arrayOfWords.length < 2) { return arrayOfWords[0]; - // } else if () { - } else { - return arrayOfWords[highestScoreIndex]; + return arrayOfWords[highestScoreIndex]; } } + }; Scrabble.Player = class { @@ -105,14 +112,14 @@ Scrabble.Player = class { } play(word) { - if (this.hasWon() === true) { - return false; - } - if (typeof word !== 'string') { - throw 'Error'; - } else { - return this.plays.push(word); - } + if (this.hasWon() === true) { + return false; + } + if (typeof word !== 'string') { + throw 'Error'; + } else { + return this.plays.push(word); + } } totalScore() {