Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jessica's JS Scrabble #24

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 112 additions & 2 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,117 @@
var Scrabble = function() {};
// 'use strict';
// var prompt = require('prompt');
//
// prompt.start();

// var Scrabble = prompt.get(['word'], function(err, result) {
// this._word = result.word.toUpperCase();
// });

var letters = {
"AEIOULNRST": 1,
"DG": 2,
"BCMP": 3,
"FHVWY": 4,
"K": 5,
"JX": 8,
"QZ": 10
};

var Scrabble = { //is it possible to initialize this as a constructor? function()

};

Scrabble.score = function(entry) {
var total = 0;
var word = entry.toUpperCase();
if (word.length == 7) {
total = 50;
}
for (var i = 0; i < word.length; i++) {
for (var group in letters) {
if (group.includes(word[i])) {
total += letters[group];
}
}
}
return total;
};

Scrabble.highestScoreFrom = function(arrayOfWords) {
var bestScore = 0;
var bestWord = "";
arrayOfWords.forEach(function(word) {
if (Scrabble.score(word) > bestScore) {
bestScore = Scrabble.score(word);
bestWord = word;
} else if (Scrabble.score(word) == bestScore) {
if (bestWord.length > word.length && bestWord.length != 7) {
bestWord = word;
} else if (word.length > bestWord.length && word.length == 7) {
bestWord = word;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this series of if/else could be simplified. Think about it a little.

}
});
return bestWord;
};

var Player = function(name) {
this._name = name;
this.plays = [];
};

Player.prototype = Scrabble; //I thought this would make the Player object inherit form Scrabble, but it basically does nothing. If I take it out, everything works the same...

Player.prototype.play = function(word) {
if (this.hasWon() === true) {
return false;
}
this.plays.push(word);
};

Player.prototype.totalScore = function() {
var totalScore = 0;
this.plays.forEach(function(word){
totalScore += Scrabble.score(word);
});
return totalScore;
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI, the forEach function above is depreciated, but it will still work fine.


Player.prototype.hasWon = function() {
if (this.totalScore() >= 100) {
return true;
} else {
return false;
}
};

Player.prototype.highestScoringWord = function() {
return Scrabble.highestScoreFrom(this.plays);
};

Player.prototype.highestWordScore = function() {
return Scrabble.score(this.highestScoringWord());
};

jess = new Player("Jessica");
jess.play("hi");
jess.play("another");
console.log(jess);
console.log(jess.totalScore());
console.log(jess.hasWon());
console.log(jess.highestScoringWord());
// console.log(jess.highestWordScore());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work putting code to test things.


console.log(Scrabble.score("hi")); //5
console.log(Scrabble.score("elevens")); //60
console.log(Scrabble.highestScoreFrom(["hello", "blah", "test", "elevens"])); //should return "elevens"
console.log(Scrabble.highestScoreFrom(["hi", "dig"])); //should return "hi"
console.log(Scrabble.highestScoreFrom(["lost", "tons"])); //should return "lost"
console.log(Scrabble.highestScoreFrom(["AEIOULD", "QZQZQJ"])); //should return "AEIOULD"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should have a few more tests to test the different possibilities for highestScoreFrom.

Testing in all different orders with:
A 7-letter word
Multiple 7-letter words
Multiple 7-letter words with tied scores.
Word that tie in score
Words that tie in score and are the same length.

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
Scrabble.helloWorld = function() {
return 'hello world!';
};

Expand Down