From 05ad86efeb850bd6c07bcdd7eccb241811168167 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Tue, 14 Nov 2017 19:24:45 -0800 Subject: [PATCH 1/3] score function added --- package-lock.json | 123 ++++++++++++++++++++++++++++++++++++++++++++++ scrabble.js | 76 +++++++++++++++++++++++++--- 2 files changed, 191 insertions(+), 8 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..bca41f4 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,14 +1,74 @@ +// try { +// if(x == "") throw "empty"; +// if(isNaN(x)) throw "not a number"; +// x = Number(x); +// if(x < 5) throw "too low"; +// if(x > 10) throw "too high"; +// } +// catch(err) { +// message.innerHTML = "Input is " + err; +// } +// do this inside of your stuff +// +// const UserException = function UserException(message) { +// this.message = message; +// this.name = 'UserException'; +// } +// throw new ScrabbleException('InvalidMonthNo'); + const Scrabble = { + score: function(word) { - // TODO: implement score - } + let wordArray = word.split(''); + let letter_points = { '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 score = 0; + if (wordArray.length === 7){ + score += 50; + } else if (wordArray.length > 7){ + throw `This word is too long. Are you sure you aren't cheating?`; + } else if (wordArray.length === 0){ + throw 'You have to choose a word.' + } else{} + + for (let letter of wordArray){ + + let point = letter_points[letter.toUpperCase()]; + // try { + if(point === undefined) { + throw 'not found';} + // } + // catch(err) { + // message.innerHTML = "Input is " + err; + // } + + score += point; + } + + return score; + }, + highestScoreFrom: function(arrayOfWords) { + if (arrayOfWords.length === 0 || arrayOfWords === ""){ + throw 'The input is not what I expected.' + } else if (arrayOfWords.length === 1){ + return arrayOfWords[0]; + } + let highestScore = 0; + let highestScoringWord = ''; + for (let word of arrayOfWords){ + let wordScore = this.score(word); - // TODO: add the highestScoreFrom method -}; + if (wordScore > highestScore || (highestScore === wordScore && word.length < highestScoringWord.length && highestScoringWord.length != 7) || (highestScore === wordScore && word.length === 7)){ + highestScore = wordScore; + highestScoringWord = word; + } else{} + } + return highestScoringWord; + } + }; -Scrabble.Player = class { - // TODO: implement the Player class -}; + Scrabble.Player = class { + // TODO: implement the Player class + }; -module.exports = Scrabble; + module.exports = Scrabble; From 20e0833c9236bdaf8bac08afb59b148f772e0a4d Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 15 Nov 2017 12:13:54 -0800 Subject: [PATCH 2/3] adding totalScore, playWord, and player functions to scrabble --- scrabble.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index bca41f4..e882beb 100644 --- a/scrabble.js +++ b/scrabble.js @@ -48,7 +48,7 @@ const Scrabble = { }, highestScoreFrom: function(arrayOfWords) { if (arrayOfWords.length === 0 || arrayOfWords === ""){ - throw 'The input is not what I expected.' + throw 'The input is not what I expected.'; } else if (arrayOfWords.length === 1){ return arrayOfWords[0]; } @@ -68,7 +68,35 @@ const Scrabble = { }; Scrabble.Player = class { - // TODO: implement the Player class + constructor(name) { + this.name = name; + this.plays = []; + if (this.name === null || this.name === '' || this.name === undefined){ + throw 'A Scrabble player needs a name'; + } + } + + totalScore(){ + let totalScore = 0; + for(let word in this.plays){ + totalScore += Scrabble.score(word); + } + return totalScore; + } + + hasWon(){ + + } + + play(word){ + let matcher = /[A-Za-z]+/; + let matches = word.match(matcher); + if (word === undefined || matches[0].length != word.length){ + throw 'You have to give a REAL word.'; + } + this.plays.push(word); + return word + } }; module.exports = Scrabble; From 1a6ac4bc1adfa8bc69e865687bd0326d88432563 Mon Sep 17 00:00:00 2001 From: Roxanne Date: Wed, 15 Nov 2017 14:25:17 -0800 Subject: [PATCH 3/3] finished wave 2 --- scrabble.js | 129 +++++++++++++++++++++++++----------------- spec/scrabble_spec.js | 1 + 2 files changed, 78 insertions(+), 52 deletions(-) diff --git a/scrabble.js b/scrabble.js index e882beb..023992a 100644 --- a/scrabble.js +++ b/scrabble.js @@ -27,76 +27,101 @@ const Scrabble = { } else if (wordArray.length > 7){ throw `This word is too long. Are you sure you aren't cheating?`; } else if (wordArray.length === 0){ - throw 'You have to choose a word.' + throw 'You have to choose a word.'; } else{} - for (let letter of wordArray){ + + for (let letter of wordArray){ let point = letter_points[letter.toUpperCase()]; // try { if(point === undefined) { - throw 'not found';} - // } - // catch(err) { - // message.innerHTML = "Input is " + err; - // } - - score += point; + throw 'not found'; } - return score; - }, - highestScoreFrom: function(arrayOfWords) { - if (arrayOfWords.length === 0 || arrayOfWords === ""){ - throw 'The input is not what I expected.'; - } else if (arrayOfWords.length === 1){ - return arrayOfWords[0]; - } - let highestScore = 0; - let highestScoringWord = ''; - for (let word of arrayOfWords){ - let wordScore = this.score(word); + score += point; + } + + return score; + }, + highestScoreFrom: function(arrayOfWords) { + if (arrayOfWords.length === 0 || arrayOfWords === "") { + throw 'The input is not what I expected.'; + } else if (arrayOfWords.length === 1){ + return arrayOfWords[0]; + } + let highestScore = 0; + let highestScoringWord = ''; + for (let word of arrayOfWords){ + let wordScore = this.score(word); - if (wordScore > highestScore || (highestScore === wordScore && word.length < highestScoringWord.length && highestScoringWord.length != 7) || (highestScore === wordScore && word.length === 7)){ - highestScore = wordScore; - highestScoringWord = word; - } else{} + if (wordScore > highestScore || (highestScore === wordScore && word.length < highestScoringWord.length && highestScoringWord.length != 7) || (highestScore === wordScore && word.length === 7)){ + highestScore = wordScore; + highestScoringWord = word; } - return highestScoringWord; } - }; - - Scrabble.Player = class { - constructor(name) { - this.name = name; - this.plays = []; - if (this.name === null || this.name === '' || this.name === undefined){ - throw 'A Scrabble player needs a name'; - } + return highestScoringWord; + } + +}; + +Scrabble.Player = class { + constructor(name) { + this.name = name; + this.plays = []; + if (this.name === null || this.name === '' || this.name === undefined){ + throw 'A Scrabble player needs a name'; } + } - totalScore(){ - let totalScore = 0; - for(let word in this.plays){ - totalScore += Scrabble.score(word); - } - return totalScore; + totalScore() { + let score = 0; + + if (this.plays.length === 0) { + return score; + } + for(let word of this.plays){ + score += Scrabble.score(word); } + return score; + } - hasWon(){ - + hasWon(){ + let winningPoints = 100; + let currentScore = this.totalScore(); + if(currentScore >= winningPoints){ + return true; } + return false; + } - play(word){ - let matcher = /[A-Za-z]+/; - let matches = word.match(matcher); - if (word === undefined || matches[0].length != word.length){ - throw 'You have to give a REAL word.'; - } - this.plays.push(word); - return word + play(word){ + let matcher = /[A-Za-z]+/; + let matches = word.match(matcher); + if (word === undefined || matches[0].length != word.length){ + throw 'You have to give a REAL word.'; + } + let won = this.hasWon(); + if( won === true){ + return false; } - }; + this.plays.push(word); + return word; + } + + + highestScoringWord(){ + let playArray = this.plays; + let highestWord = Scrabble.highestScoreFrom(playArray); + return highestWord; + } + + highestWordScore(){ + let highestWord = this.highestScoringWord(); + let highestScore = Scrabble.score(highestWord); + return highestScore; + } +}; module.exports = Scrabble; diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..0cddf3a 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -145,6 +145,7 @@ describe('Player', function() { expect(player.play('dog')).toBe(false); expect(player.plays.length).toBe(1); + }); });