From 909e08404fa6b656dc40535ce3e27ad3217c8729 Mon Sep 17 00:00:00 2001 From: Kayla Ecker Date: Fri, 17 Nov 2017 09:03:13 -0800 Subject: [PATCH 1/2] baseline met, forgot to commit until submitting pull request --- scrabble.js | 133 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 9 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7a1f161..8eaa3fc 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,14 +1,129 @@ +const scrabbleLetters = { + 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) { - // TODO: implement score - } + score: function(word) { + + let regex = /^[a-zA-Z]+$/; + + if (!regex.test(word)) { + throw new Error('Invalid characters'); + } + + if (word.length > 7) { + throw new Error('Word must be 1 - 7 characters long'); + } + + let lowercaseWord = word.toLowerCase(); + let score = 0; + + + for (let i = 0; i < lowercaseWord.length; i++) { + score += scrabbleLetters[lowercaseWord[i]]; + } + + if (lowercaseWord.length === 7) { + score += 50; + } + + return score; + }, + + highestScoreFrom: function(wordsArray) { + + if (!Array.isArray(wordsArray) || wordsArray.length === 0 ) { + throw new Error('Invalid'); + } + + if (wordsArray.length === 1) { + return wordsArray[0]; + } + + let max = 0; + let highestWord = ''; + + for (let i = 0; i < wordsArray.length; i++) { + let score = this.score(wordsArray[i]); + + if (score > max) { + max = score; + highestWord = wordsArray[i]; + } + else if (score === max) { + if (highestWord.length !== 7 && + (wordsArray[i].length === 7 || (wordsArray[i].length < highestWord.length))) { + highestWord = wordsArray[i]; + } + } + } + + return highestWord; + }, + + + }; + + Scrabble.Player = class { + constructor(name) { + if (name === undefined) { + throw new Error('Invalid - must be a name'); + } + this.name = name; + this.playArray = []; + this.plays = this.playArray; + } + + play(word) { + let regex = /^[a-zA-Z]+$/; + + if (word === undefined || !regex.test(word)) { + throw new Error('Invalid word'); + } + + if (! this.winning()) { + if (this.playArray.push(word)) { + return true; + } + } else { + return false; + } + } + + totalScore() { + let total = 0; + + for (let i = 0; i < this.plays.length; i++) { + total += Scrabble.score(this.plays[i]); + } + + return total; + } + + winning() { + + if (this.totalScore() >= 100) { + return true; + } else { + return false; + } + } + + highestScoringWord() { + let example = Scrabble.highestScoreFrom(this.plays); + return example; + } - // TODO: add the highestScoreFrom method + highestWordScore() { + let highestWord = this.highestScoringWord(); + return Scrabble.score(highestWord); + } + }; -}; -Scrabble.Player = class { - // TODO: implement the Player class -}; -module.exports = Scrabble; + module.exports = Scrabble; From 27e2a329eaafe68e95c5d47bf83c3a24b517dffc Mon Sep 17 00:00:00 2001 From: Kayla Ecker Date: Fri, 17 Nov 2017 09:09:19 -0800 Subject: [PATCH 2/2] passing tests --- scrabble.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8eaa3fc..ad5aaa1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -85,7 +85,7 @@ const Scrabble = { throw new Error('Invalid word'); } - if (! this.winning()) { + if (! this.hasWon()) { if (this.playArray.push(word)) { return true; } @@ -104,7 +104,7 @@ const Scrabble = { return total; } - winning() { + hasWon() { if (this.totalScore() >= 100) { return true;