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

Queues - Sahana Murthy - Scrabble JS #39

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var Scrabble = require('./scrabble');

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

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

// Function which sums up and returns the score of the players words
Player.prototype.totalScore = function() {
var sum = 0;
this.plays.forEach(function(word) {
sum += Scrabble.prototype.score(word);
});
return sum;
};

// hasWon(): Function which returns true if the player has over 100 points, otherwise returns false
Player.prototype.hasWon = function() {
if (this.totalScore >= 100) {
return true;
} else {
return false;
};
};

// highestScoringWord(): Function which returns the highest scoring word the user has played
Player.prototype.highestScoringWord = function() {
word = Scrabble.prototype.highestScoreFrom(this.plays);
return word;
};

// highestWordScore(): Function which returns the highestScoringWord score
Player.prototype.highestWordScore = function() {
var word = this.highestScoringWord(this.plays);
var score = Scrabble.prototype.score(word);
return score;
};

var me = new Player("Sahana");
console.log(me.plays);
console.log(me.play("Hello"));
console.log(me.play("Hi"));
console.log(me.plays);
console.log(me.totalScore());
console.log(me.hasWon());
console.log(me.highestScoringWord());
console.log(me.highestWordScore());
71 changes: 67 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
var Scrabble = function() {};

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

Scrabble.prototype.score = function(word) {
var scoreChart = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10};

word = word.toLowerCase();
var wordScore = 0;

for (var i = 0; i < word.length; i++) {
var letterScore = scoreChart[word.charAt(i)];
wordScore += letterScore;
}

word.length == 7 ? wordScore += 50 : wordScore;

return wordScore;

};


Scrabble.prototype.highestScoreFrom = function(arrayOfWords) {

var highestWord = "";
var self = this;
var highestScore = 0;
var highestScores = [];

arrayOfWords.forEach(function(word) {
var score = self.score(word);

if (score == highestScore) {
highestScores.push(word);
} else if (score > highestScore) {
highestScores = [];
highestScores.push(word);
highestScore = score;
};
});

console.log(highestScores);

if (highestScores.length == 1) {
highestWord = highestScores[0];
return highestWord;
} else if (highestScores.length > 1) {
for (i = 0; i < highestScores.length; i++) {
if (highestScores[i].length == 7) {
highestWord = highestScores[i];
} else {
var shortestWord = highestScores[0];
if(highestScores[i].length < shortestWord.length) {
shortestWord = highestScores[i];
highestWord = shortestWord;
};
};
return highestWord;
};
};
};


var game = new Scrabble();

console.log(game.highestScoreFrom(["HI", "moo", "eo", "q"]));

module.exports = Scrabble;