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

Maya's JS Scrabble - completed (parens) #23

Open
wants to merge 3 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
66 changes: 66 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var scrabble = require('./scrabble')

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

this.totalScore = function() {
var total = 0;

this.plays.forEach(function(word){
total += scrabble.score(word)
});

return total;
};

this.hasWon = function() {
return this.totalScore() > 100;
};

this.highestScoringWord = function() {
return scrabble.highestScoreFrom(this.plays);
};

this.highestWordScore = function() {
return scrabble.score(this.highestScoringWord());
};
}

Player.prototype.play = function(word) {
this.plays.push(word);
};

module.export = Player;

// only run test cases if this file is directly invoked by NodeJS
if (require.main === module) {
var player1 = new Player('p1');

[
'all',
'your',
'bases',
'are',
'belongs',
'to',
'us',
'rinse',
'repeat',
'and',
'follows',
'again'
].forEach(function(word) {
console.log('============ Player ' + player1.name + ' ================');
player1.play(word);
console.log('Played word ' + word);
console.log('All played words so far ' + player1.plays);
console.log('Total score ' + player1.totalScore());
console.log('Highest Scoring Word ' + player1.highestScoringWord());
console.log('Highest Word Score ' + player1.highestWordScore());

if (player1.hasWon()) {
console.log('Player ' + player1.name + ' has won');
}
});
}
136 changes: 132 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,136 @@
var Scrabble = function() {};
var SCORE_CHART = {
"A":1,"E":1,"I":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
}

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

Scrabble.score = function(word) {
var letters = word.toUpperCase().split(""); // return an array of letters in the word

var totalScore = 0;
letters.forEach(function (letter){
totalScore += SCORE_CHART[letter];
});

if ( word.length == 7 ) {
totalScore += 50;
}

return totalScore;
}

Scrabble.highestScoreFrom = function(arrayOfWords) {
var highestScore = 0;
var highestScoreWords = [];

arrayOfWords.forEach(function (word) {
var score = Scrabble.score(word)

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

// Here highestScoreWords contains all the word(s) (tied) for the highest score

// if no tie, directly return the highest_score_word
if (highestScore.length === 1) {
return highestScore[0];
}

// if there is a tie or multiple ties, consider if there is a 7-letter-word
lengthSevenWords = []
// console.log("highestScoreWords " + highestScoreWords)
highestScoreWords.forEach(function (word) {
// if so, return the first 7-letter-word
if (word.length === 7){
lengthSevenWords.push(word);
}
});

// console.log("lengthSevenWords " + lengthSevenWords)
if (lengthSevenWords.length > 0) {
return lengthSevenWords[0];
}

// if there is no 7-letter-word, return the minimun length word
var lengths = []
highestScoreWords.forEach(function (word) {
lengths.push(word.length);
});

var minLength = Math.min(...lengths);

var minLengthWords = []
highestScoreWords.forEach(function (word) {
if ( word.length === minLength ) {
minLengthWords.push(word)
}
});

return minLengthWords[0]
};

module.exports = Scrabble;

// only run test cases if this file is directly invoked by NodeJS
if (require.main === module) {
// manual testing
// Testing score method
[
["all", 3],
["your", 7],
["bases", 7],
["are", 3],
["belongs", 60],
["to", 2],
["us", 2],
].forEach(function(testCase) {
var word = testCase[0];
var expectedScore = testCase[1];
var actualScore = Scrabble.score(word);

if (expectedScore !== actualScore ) {
console.warn("wrong score for " + word);
}

console.log("score for word " + word + " is " + actualScore + " expected score is " + expectedScore);
});

// Testing highestScoreWords methods
[
// highest score word wins
[["bc", "dg"], "bc"],
// same score, shorter word wins
[["all", "ad"], "ad"],
// same score, same length, first word wins
[["all", "alo"], "all"],
// given a tie, 7 letter word wins
[["qzqzqz", "belongs"], "belongs"],

// given a tie, an more than one 7 letter words, first
// 7 letter word wins
[["qzqzqz", "belongs", "balongs"], "belongs"],
].forEach(function(testCase) {
var words = testCase[0];
var expectedWord = testCase[1];
var actualWord = Scrabble.highestScoreFrom(words);

if (expectedWord !== actualWord ) {
console.warn("wrong word for " + words);
}

console.log("highest score word among " + words + " is " + actualWord);
});
}