From 531257274471ef76c338b109540aa7295936e49e Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Tue, 15 Nov 2016 15:08:24 -0800 Subject: [PATCH 1/6] Added initial take on Wave 1 --- scrabble.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index a7d0745..435bcaa 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,59 @@ -var Scrabble = function() {}; +var Scrabble = function() { + this.score = function(word) { + var cases = {A: 1, E: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1, D: 2, G: 2, B: 3, C: 3, M: 3, P: 3, F: 4, H: 4, V: 4, W: 4, Y: 4, K: 5, J: 8, X: 8, Q: 10, Z: 10}; + var letters = word.toUpperCase().split(''); + var total = 0; + + if (word.length == 7) { + total += 50; + } + + for (var i = 0; i < letters.length; i++) { + var letter = letters[i]; + total += cases[letter]; + } + + return total; + }; + + this.highestScoreFrom = function(arrayOfWords) { + var scoredWords = {}; + + //Adds word and its score to scoredWords + for (var i = 0; i < arrayOfWords.length; i++) { + var score = this.score(arrayOfWords[i]); + scoredWords[arrayOfWords[i]] = score; + } + + var winningWord = [null, 0]; + //Looks through scoredWords for highest scoring word + for (var key in scoredWords) { + if (scoredWords[key] > winningWord[1]) { + winningWord[0] = key; + winningWord[1] = scoredWords[key]; + } + else if (scoredWords[key] == winningWord[1]) { + if (key.length == 7 || key.length < winningWord[1].length) { + winningWord[0] = key; + winningWord[1] = scoredWords[key]; + } + } + } + + return winningWord[0]; + }; +}; -// YOUR CODE HERE Scrabble.prototype.helloWorld = function() { return 'hello world!'; }; module.exports = Scrabble; + +var newGame = new Scrabble(); +var wordScore = newGame.score("aaaaaaa"); +console.log(wordScore); + +var anotherGame = new Scrabble(); +var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); +console.log(bestWord); From b80df3d3a0d2eafe3aa20926ac2fef16684ca3f5 Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Tue, 15 Nov 2016 16:12:58 -0800 Subject: [PATCH 2/6] Initial take on Wave 2 added --- scrabble.js | 87 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/scrabble.js b/scrabble.js index 435bcaa..afd5ef3 100644 --- a/scrabble.js +++ b/scrabble.js @@ -25,17 +25,20 @@ var Scrabble = function() { scoredWords[arrayOfWords[i]] = score; } - var winningWord = [null, 0]; + var winningWord = null; + var winningScore = null; //Looks through scoredWords for highest scoring word for (var key in scoredWords) { - if (scoredWords[key] > winningWord[1]) { - winningWord[0] = key; - winningWord[1] = scoredWords[key]; + if (scoredWords[key] > winningScore) { + winningWord = key; + winningScore = scoredWords[key]; } - else if (scoredWords[key] == winningWord[1]) { - if (key.length == 7 || key.length < winningWord[1].length) { - winningWord[0] = key; - winningWord[1] = scoredWords[key]; + else if (scoredWords[key] == winningScore) { + if (key.length == 7 || key.length < winningWord.length) { + if (winningWord.length != 7) { + winningWord = key; + winningScore = scoredWords[key]; + } } } } @@ -44,16 +47,64 @@ var Scrabble = function() { }; }; -Scrabble.prototype.helloWorld = function() { - return 'hello world!'; -}; -module.exports = Scrabble; +// Create a new `Player` object. The object should have the following functions: +// +// - Constructor: Called when you use `new Player(name)`, sets up an instance with the instance variable `name` assigned +// - `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)`: Function which adds the input word to the `plays` Array +// - Returns false if player has already won +// - `totalScore()`: Function which sums up and returns the score of the players words +// - `hasWon()`: Function which returns `true` if the player has over 100 points, otherwise returns `false` +// - `highestScoringWord()`: Function which returns the highest scoring word the user has played +// - `highestWordScore()`: Function which returns the `highestScoringWord` score + +var Player = function(name) { + this.name = name; + this.plays = []; + this.play = function(word) { + if (hasWon === true) { + return false; + } + else { + this.plays.push(word); + } + }; + this.totalScore = function() { + var playersScore = 0; + for (var i = 0; i < this.plays.length; i++) { + total += Scrabble.score(this.plays[i]); + } + return playersScore; + }; + this.hasWon = function() { + if (Player.totalScore() > 100) { + return true; + } + else { + return false; + } + }; + this.highestScoringWord = function() { + return Scrabble.highestScoreFrom(plays); + }; + this.highestWordScore = function() { + var word = Scrabble.highestScoreFrom(plays); + return Scrabble.highestWordScore(word); + }; +}; -var newGame = new Scrabble(); -var wordScore = newGame.score("aaaaaaa"); -console.log(wordScore); +// Scrabble.prototype.helloWorld = function() { +// return 'hello world!'; +// }; +// +// module.exports = Scrabble; -var anotherGame = new Scrabble(); -var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); -console.log(bestWord); +// var newGame = new Scrabble(); +// var wordScore = newGame.score("aaaaaaa"); +// console.log(wordScore); +// +// var anotherGame = new Scrabble(); +// var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); +// console.log(bestWord); From 5b84458eda434ea2db8a15744fa054a148ee8b53 Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Tue, 15 Nov 2016 22:12:25 -0800 Subject: [PATCH 3/6] Ran the code a few times with example calls and fixed a few errors (mostly calling the other functions incorrectly). Seems to work decently right now, need to check again. --- scrabble.js | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/scrabble.js b/scrabble.js index afd5ef3..748d964 100644 --- a/scrabble.js +++ b/scrabble.js @@ -43,28 +43,24 @@ var Scrabble = function() { } } - return winningWord[0]; + return winningWord; }; }; - -// Create a new `Player` object. The object should have the following functions: +// var newGame = new Scrabble(); +// var wordScore = newGame.score("aaaaaaa"); +// console.log(wordScore); // -// - Constructor: Called when you use `new Player(name)`, sets up an instance with the instance variable `name` assigned -// - `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)`: Function which adds the input word to the `plays` Array -// - Returns false if player has already won -// - `totalScore()`: Function which sums up and returns the score of the players words -// - `hasWon()`: Function which returns `true` if the player has over 100 points, otherwise returns `false` -// - `highestScoringWord()`: Function which returns the highest scoring word the user has played -// - `highestWordScore()`: Function which returns the `highestScoringWord` score +// var anotherGame = new Scrabble(); +// var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); +// console.log(bestWord); + var Player = function(name) { this.name = name; this.plays = []; this.play = function(word) { - if (hasWon === true) { + if (this.hasWon() === true) { return false; } else { @@ -73,13 +69,14 @@ var Player = function(name) { }; this.totalScore = function() { var playersScore = 0; + for (var i = 0; i < this.plays.length; i++) { - total += Scrabble.score(this.plays[i]); + playersScore += new Scrabble().score(this.plays[i]); } return playersScore; }; this.hasWon = function() { - if (Player.totalScore() > 100) { + if (this.totalScore() > 100) { return true; } else { @@ -87,24 +84,26 @@ var Player = function(name) { } }; this.highestScoringWord = function() { - return Scrabble.highestScoreFrom(plays); + return new Scrabble().highestScoreFrom(this.plays); }; this.highestWordScore = function() { - var word = Scrabble.highestScoreFrom(plays); - return Scrabble.highestWordScore(word); + var word = this.highestScoringWord(this.plays); + return new Scrabble().score(word); }; }; +// var newPlayer = new Player("briana"); +// console.log(newPlayer.name); +// newPlayer.play("test"); +// newPlayer.play("xxxxx"); +// console.log(newPlayer.plays); +// console.log(newPlayer.totalScore()); +// console.log(newPlayer.hasWon()); +// console.log(newPlayer.highestScoringWord()); +// console.log(newPlayer.highestWordScore()); + // Scrabble.prototype.helloWorld = function() { // return 'hello world!'; // }; // // module.exports = Scrabble; - -// var newGame = new Scrabble(); -// var wordScore = newGame.score("aaaaaaa"); -// console.log(wordScore); -// -// var anotherGame = new Scrabble(); -// var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); -// console.log(bestWord); From 22506eeed1f57f046b3b558750e5ce48f9389db3 Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Wed, 16 Nov 2016 11:37:16 -0800 Subject: [PATCH 4/6] Added comments, reverted all to inner functions --- scrabble.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/scrabble.js b/scrabble.js index 748d964..47836d9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,13 +1,17 @@ +//WAVE 1 + var Scrabble = function() { this.score = function(word) { var cases = {A: 1, E: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1, D: 2, G: 2, B: 3, C: 3, M: 3, P: 3, F: 4, H: 4, V: 4, W: 4, Y: 4, K: 5, J: 8, X: 8, Q: 10, Z: 10}; var letters = word.toUpperCase().split(''); var total = 0; + //Adds 50 pt bonus for a seven letter word if (word.length == 7) { total += 50; } + //Adds up letter points for full word score for (var i = 0; i < letters.length; i++) { var letter = letters[i]; total += cases[letter]; @@ -55,11 +59,13 @@ var Scrabble = function() { // var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); // console.log(bestWord); +//WAVE 2 var Player = function(name) { this.name = name; this.plays = []; this.play = function(word) { + //Checks if player already won, adds word to plays if they haven't if (this.hasWon() === true) { return false; } @@ -70,12 +76,14 @@ var Player = function(name) { this.totalScore = function() { var playersScore = 0; + //Scores each of the player's plays to get total score for (var i = 0; i < this.plays.length; i++) { playersScore += new Scrabble().score(this.plays[i]); } return playersScore; }; this.hasWon = function() { + //Checks to see if player's score is over 100 (aka if they won) if (this.totalScore() > 100) { return true; } @@ -84,10 +92,13 @@ var Player = function(name) { } }; this.highestScoringWord = function() { + //Returns highest scoring word from the player's plays return new Scrabble().highestScoreFrom(this.plays); }; this.highestWordScore = function() { + //Gets the player's highest scoring word var word = this.highestScoringWord(this.plays); + //Returns the score of the word return new Scrabble().score(word); }; }; @@ -106,4 +117,4 @@ var Player = function(name) { // return 'hello world!'; // }; // -// module.exports = Scrabble; +module.exports = Scrabble; From c4bab3a1d6c7a3079feadfea70c960c4c3e789f4 Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Wed, 16 Nov 2016 11:37:46 -0800 Subject: [PATCH 5/6] Added file with functions via prototype instead of inner functions --- scrabble_prototype.js | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 scrabble_prototype.js diff --git a/scrabble_prototype.js b/scrabble_prototype.js new file mode 100644 index 0000000..eb4a5e3 --- /dev/null +++ b/scrabble_prototype.js @@ -0,0 +1,125 @@ +//WAVE 1 + +var Scrabble = function() {}; + +Scrabble.prototype.score = function(word) { + var cases = {A: 1, E: 1, O: 1, U: 1, L: 1, N: 1, R: 1, S: 1, T: 1, D: 2, G: 2, B: 3, C: 3, M: 3, P: 3, F: 4, H: 4, V: 4, W: 4, Y: 4, K: 5, J: 8, X: 8, Q: 10, Z: 10}; + var letters = word.toUpperCase().split(''); + var total = 0; + + //Adds 50 pt bonus for a seven letter word + if (word.length == 7) { + total += 50; + } + + //Adds up letter points for full word score + for (var i = 0; i < letters.length; i++) { + var letter = letters[i]; + total += cases[letter]; + } + + return total; + }; + +Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { + var scoredWords = {}; + + //Adds word and its score to scoredWords + for (var i = 0; i < arrayOfWords.length; i++) { + var score = this.score(arrayOfWords[i]); + scoredWords[arrayOfWords[i]] = score; + } + + var winningWord = null; + var winningScore = null; + //Looks through scoredWords for highest scoring word + for (var key in scoredWords) { + if (scoredWords[key] > winningScore) { + winningWord = key; + winningScore = scoredWords[key]; + } + else if (scoredWords[key] == winningScore) { + if (key.length == 7 || key.length < winningWord.length) { + if (winningWord.length != 7) { + winningWord = key; + winningScore = scoredWords[key]; + } + } + } + } + + return winningWord; + }; + +// var newGame = new Scrabble(); +// var wordScore = newGame.score("aaaaaaa"); +// console.log(wordScore); +// +// var anotherGame = new Scrabble(); +// var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); +// console.log(bestWord); + +//WAVE 2 + +var Player = function(name) { + this.name = name; + this.plays = []; +}; + +Player.prototype.play = function(word) { + //Checks if player already won, adds word to plays if they haven't + if (this.hasWon() === true) { + return false; + } + else { + this.plays.push(word); + } +}; + +Player.prototype.totalScore = function() { + var playersScore = 0; + + //Scores each of the player's plays to get total score + for (var i = 0; i < this.plays.length; i++) { + playersScore += new Scrabble().score(this.plays[i]); + } + return playersScore; +}; + +Player.prototype.hasWon = function() { + //Checks to see if player's score is over 100 (aka if they won) + if (this.totalScore() > 100) { + return true; + } + else { + return false; + } +}; + +Player.prototype.highestScoringWord = function() { + //Returns highest scoring word from the player's plays + return new Scrabble().highestScoreFrom(this.plays); +}; + +Player.prototype.highestWordScore = function() { + //Gets the player's highest scoring word + var word = this.highestScoringWord(this.plays); + //Returns the score of the word + return new Scrabble().score(word); + }; + +// var newPlayer = new Player("briana"); +// console.log(newPlayer.name); +// newPlayer.play("test"); +// newPlayer.play("xxxxx"); +// console.log(newPlayer.plays); +// console.log(newPlayer.totalScore()); +// console.log(newPlayer.hasWon()); +// console.log(newPlayer.highestScoringWord()); +// console.log(newPlayer.highestWordScore()); + +// Scrabble.prototype.helloWorld = function() { +// return 'hello world!'; +// }; +// +module.exports = Scrabble; From be7a5837f766002d48975311238c5d7b0f9a547b Mon Sep 17 00:00:00 2001 From: Briana Eng Date: Thu, 17 Nov 2016 15:55:55 -0800 Subject: [PATCH 6/6] Added more examples (only to scrabble.js) and modified how Scrabble functions were called (in both). --- scrabble.js | 83 ++++++++++++++++++++++++++++--------------- scrabble_prototype.js | 25 ++++++------- 2 files changed, 68 insertions(+), 40 deletions(-) diff --git a/scrabble.js b/scrabble.js index 47836d9..75ddd37 100644 --- a/scrabble.js +++ b/scrabble.js @@ -6,18 +6,23 @@ var Scrabble = function() { var letters = word.toUpperCase().split(''); var total = 0; - //Adds 50 pt bonus for a seven letter word - if (word.length == 7) { - total += 50; + if (word.length > 7) { + return "Error. Incorrect number of tiles."; } + else { + //Adds 50 pt bonus for a seven letter word + if (word.length == 7) { + total += 50; + } - //Adds up letter points for full word score - for (var i = 0; i < letters.length; i++) { - var letter = letters[i]; - total += cases[letter]; - } + //Adds up letter points for full word score + for (var i = 0; i < letters.length; i++) { + var letter = letters[i]; + total += cases[letter]; + } - return total; + return total; + } }; this.highestScoreFrom = function(arrayOfWords) { @@ -51,19 +56,30 @@ var Scrabble = function() { }; }; -// var newGame = new Scrabble(); -// var wordScore = newGame.score("aaaaaaa"); -// console.log(wordScore); -// -// var anotherGame = new Scrabble(); -// var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); -// console.log(bestWord); +var newGame = new Scrabble(); +console.log("Should be 57 points (7 + 50 bonus)"); +var wordScore = newGame.score("aaaaaaa"); +console.log(wordScore); + +console.log("Should print error message"); +var wordScore2 = newGame.score("aaaaaaaa"); +console.log(wordScore2); + +var anotherGame = new Scrabble(); +console.log("Should print xxxxx - highest scored word"); +var bestWord = anotherGame.highestScoreFrom(["xxxx", "hello", "aaaaa", "bloop", "xxxxx"]); +console.log(bestWord); + +console.log("Should print z - same score, shorter word"); +var bestWord2 = anotherGame.highestScoreFrom(["z", "kk"]); +console.log(bestWord2); //WAVE 2 var Player = function(name) { this.name = name; this.plays = []; + var scrabbleGame = new Scrabble(); this.play = function(word) { //Checks if player already won, adds word to plays if they haven't if (this.hasWon() === true) { @@ -78,7 +94,7 @@ var Player = function(name) { //Scores each of the player's plays to get total score for (var i = 0; i < this.plays.length; i++) { - playersScore += new Scrabble().score(this.plays[i]); + playersScore += scrabbleGame.score(this.plays[i]); } return playersScore; }; @@ -93,25 +109,36 @@ var Player = function(name) { }; this.highestScoringWord = function() { //Returns highest scoring word from the player's plays - return new Scrabble().highestScoreFrom(this.plays); + return scrabbleGame.highestScoreFrom(this.plays); }; this.highestWordScore = function() { //Gets the player's highest scoring word var word = this.highestScoringWord(this.plays); //Returns the score of the word - return new Scrabble().score(word); + return scrabbleGame.score(word); }; }; -// var newPlayer = new Player("briana"); -// console.log(newPlayer.name); -// newPlayer.play("test"); -// newPlayer.play("xxxxx"); -// console.log(newPlayer.plays); -// console.log(newPlayer.totalScore()); -// console.log(newPlayer.hasWon()); -// console.log(newPlayer.highestScoringWord()); -// console.log(newPlayer.highestWordScore()); +var newPlayer = new Player("briana"); +console.log("Should print briana"); +console.log(newPlayer.name); +newPlayer.play("test"); +newPlayer.play("xxxxx"); +console.log("Should print test and xxxxx"); +console.log(newPlayer.plays); +console.log("Should print 44"); +console.log(newPlayer.totalScore()); +console.log("Should print false"); +console.log(newPlayer.hasWon()); +console.log("Should print xxxxx"); +console.log(newPlayer.highestScoringWord()); +console.log("Should print 40"); +console.log(newPlayer.highestWordScore()); +newPlayer.play("xxxxxxx"); +console.log("Should print true"); +console.log(newPlayer.hasWon()); +console.log("Should print false"); +console.log(newPlayer.play("xxxxx")); // Scrabble.prototype.helloWorld = function() { // return 'hello world!'; diff --git a/scrabble_prototype.js b/scrabble_prototype.js index eb4a5e3..a055f74 100644 --- a/scrabble_prototype.js +++ b/scrabble_prototype.js @@ -64,6 +64,7 @@ Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { var Player = function(name) { this.name = name; this.plays = []; + this.scrabbleGame = new Scrabble(); }; Player.prototype.play = function(word) { @@ -81,7 +82,7 @@ Player.prototype.totalScore = function() { //Scores each of the player's plays to get total score for (var i = 0; i < this.plays.length; i++) { - playersScore += new Scrabble().score(this.plays[i]); + playersScore += this.scrabbleGame.score(this.plays[i]); } return playersScore; }; @@ -98,25 +99,25 @@ Player.prototype.hasWon = function() { Player.prototype.highestScoringWord = function() { //Returns highest scoring word from the player's plays - return new Scrabble().highestScoreFrom(this.plays); + return this.scrabbleGame.highestScoreFrom(this.plays); }; Player.prototype.highestWordScore = function() { //Gets the player's highest scoring word var word = this.highestScoringWord(this.plays); //Returns the score of the word - return new Scrabble().score(word); + return this.scrabbleGame.score(word); }; -// var newPlayer = new Player("briana"); -// console.log(newPlayer.name); -// newPlayer.play("test"); -// newPlayer.play("xxxxx"); -// console.log(newPlayer.plays); -// console.log(newPlayer.totalScore()); -// console.log(newPlayer.hasWon()); -// console.log(newPlayer.highestScoringWord()); -// console.log(newPlayer.highestWordScore()); +var newPlayer = new Player("briana"); +console.log(newPlayer.name); +newPlayer.play("test"); +newPlayer.play("xxxxx"); +console.log(newPlayer.plays); +console.log(newPlayer.totalScore()); +console.log(newPlayer.hasWon()); +console.log(newPlayer.highestScoringWord()); +console.log(newPlayer.highestWordScore()); // Scrabble.prototype.helloWorld = function() { // return 'hello world!';