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

Wave 1 #16

Open
wants to merge 8 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
88 changes: 84 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,88 @@
'use strict';

var Scrabble = function() {};

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
return 'hello world!';
Scrabble.prototype.splitWord = function(word) {
// add some data validation here later
var wordString = word.toUpperCase();
var letterArray = Array.from(wordString);
return letterArray;
}

Scrabble.prototype.score = function(word) {
// pass in splitWord
var letters = this.splitWord(word);
var wordScore = 0;
// loop over each element in letters
for (var i = 0; i < letters.length; i++) {
var letter = letters[i];
// assign a score to letter
var score = this.letterScores[letter];
// add score to running total
wordScore += score;

}
// return score for word
return wordScore;

}

Scrabble.prototype.highestScoreFrom = function(arrayOfWords) {
var highScore = 0;
var highestScoringWord = null; // using 0 should I use null instead?

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

var word = arrayOfWords[i];
var currentWordScore = this.score(word);

if (currentWordScore == highScore){
if (word.length == 7 && highestScoringWord.length != 7){
highestScoringWord = word;
// if there is a word that uses all seven tiles award that word an additional 50 pts
highScore = currentWordScore + 50;
// if there is a tie, prefer the highest scorring word made with the fewest tiles

Choose a reason for hiding this comment

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

Shouldn't the score function already count in the 50 point 7-letter-word bonus?

} else if (word.length < highestScoringWord.length) {
highestScoringWord = word;
highScore = currentWordScore;
}
}

if (currentWordScore > highScore) {
highScore = currentWordScore;
highestScoringWord = word;
}

}
return highestScoringWord;
}


// // chose to use object as these are not so much returning a actions as being acted upon
// you need the semicolon for object literals
Scrabble.prototype.letterScores = {
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 // NO SEMICOLON!
};

Choose a reason for hiding this comment

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

Very nice use of a JavaScript object as a hash!


module.exports = Scrabble;
var wordSeven = "jejunum";
var wordThree = "wee";
var oneScrabble = new Scrabble();
var arrayWords = ["Ty", "Bacon", "piecrust", "tea", "eiecrust"];

console.log(oneScrabble.score(wordSeven));
console.log(oneScrabble.score(wordThree));
console.log(oneScrabble.highestScoreFrom(arrayWords));



// var myScrabble = new Scrabble();

// do stuff here to test

module.exports = Scrabble; // take the whole scrabble class so it can be used by other files (What do you want to be visible outside the module)