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

Beylul Kbreab #25

Open
wants to merge 5 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
142 changes: 138 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,142 @@
var Scrabble = function() {};
Scrabble.prototype.score = function(word) {
var letterScore = {"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};

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

this.word = word.split("");
var score = 0;
if (this.word.length == 7) {
score = 50;

Choose a reason for hiding this comment

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

I like the way you reassigned the base score based on the length

}
this.word.forEach(function(i) {
score += letterScore[i];
});
return score;
};

Scrabble.prototype.highestScoreFrom = function(arrayOfWords)
{
this.arrayOfWords = arrayOfWords;
var scoredWords = [];
var highestScore = 0;

for (var i = 0; i < arrayOfWords.length; i++) {
var word = arrayOfWords[i];
var scoredInt = this.score(word);
if (highestScore <= scoredInt){
highestScore = scoredInt;
}
}

for (var i = 0; i < arrayOfWords.length; i++) {

Choose a reason for hiding this comment

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

This is a bit redundant since it is repeating almost all of the same logic in the loop above. Any ideas on how to consolidate?

var word = arrayOfWords[i];
var scoredInt = this.score(word);
if(scoredInt == highestScore){
scoredWords.push(word);
}
}

if(scoredWords.length > 1)
{

Choose a reason for hiding this comment

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

JS syntax is a bit weird, but we do expect the { to be on the same line as the if and the else

scoredWords.sort(function(a, b ) {
return b.length - a.length;
});
if ((scoredWords[0].length)==7) {
return scoredWords[0];
} else{
return(scoredWords[scoredWords.length - 1]);
}
}
else{
return scoredWords[0];
}
};

var Player = function(name){
this.name = name;
this.plays =[];
this.scrabble = new Scrabble();
};

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

Player.prototype.totalScore = function() {
var totalScore = 0;
for(var i = 0; i < this.plays.length; i++){
totalScore += this.scrabble.score(this.plays[i]);
console.log(this.scrabble.score(this.plays[i]));

Choose a reason for hiding this comment

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

This is good for debugging but it results in duplicating the score logic multiple times. I'd recommend assigning this value to a variable and then printing it our for performance purposes.

}
return totalScore;
};

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

Player.prototype.highestScoringWord = function(){
var highstWordScored = this.scrabble.highestScoreFrom(this.plays);
return highstWordScored;
};

Player.prototype.highestWordScore = function(){
var highstScore = this.scrabble.score(this.highestScoringWord());
return highstScore;
};

//testing player
// var player = new Player("Beylul");
// console.log(player.plays);
// console.log(player.play("word"));
// console.log(player.plays);
// console.log(player.totalScore());
// console.log(player.plays);
// console.log(player.hasWon());
// console.log(player.play("zzzzzzz"));
// console.log(player.plays);
// console.log(player.play("zzzzzzz"));
// console.log(player.totalScore());
// console.log(player.hasWon());
// console.log(player.highestScoringWord());
// console.log(player.highestWordScore());




// //testing score highestScoreFrom
// var scrabble = new Scrabble();
// console.log(scrabble.highestScoreFrom(["iiiiiii","aaa", "asefr", "aer", "aserd", "aaaaaaa", "iiiiiid"]));
// console.log(scrabble.highestScoreFrom(["dd","iiii"]));
// console.log(scrabble.highestScoreFrom(["iiii","dd"]));
//
// console.log(scrabble.highestScoreFrom(["aaa", "asefr", "aer", "aserd", "iiiiiii", "aaaaaaa"]));
// console.log(scrabble.highestScoreFrom(["aaa", "asefr", "aer", "aserd","aaaaaaa", "iiiiiii"]));
// console.log(scrabble.highestScoreFrom(["aaa", "asefr", "aer", "aserd", "aaaaaaa", "zzzzz"]));
// //
// // //testing score
// console.log(scrabble.score("iiiiiid"));
// console.log(scrabble.score("iiiiiii"));
// console.log(scrabble.score("aaaaaaa"));
// console.log(scrabble.score("zzzzz"));


// YOUR CODE HERE
// Scrabble.prototype.helloWorld = function() {
// return 'hello world!';
// };
// module.exports = Scrabble;
//
// scrabb = new Scrabble;
// console.log(scrabb.helloWorld());