-
Notifications
You must be signed in to change notification settings - Fork 44
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
Suzannah #34
base: master
Are you sure you want to change the base?
Suzannah #34
Changes from all commits
c9de1cb
8739298
0f8d35c
4ad41b7
d03f3b6
423728f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,173 @@ | ||
var Scrabble = function() {}; | ||
var Scrabble = function() { | ||
// due to how the specs are written, this score table does not account for the 50 point bonus for 7-letter words... | ||
this.scoreTable = [ | ||
[1, "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], | ||
[2, "D", "G"], | ||
[3, "B", "C", "M", "P"], | ||
[4, "F", "H", "V", "W", "Y"], | ||
[5, "K"], | ||
[8, "J", "X"], | ||
[10, "Q", "Z"] | ||
]; | ||
}; | ||
|
||
// score(word): returns the total score value for the given word. The word is input as a string (case insensitive). | ||
Scrabble.prototype.score = function(word) { | ||
var wordScore = 0; | ||
var nonAlphaChar = 0; | ||
var alphaChars = ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T", "D", "G", "B", "C", "M", "P", "F", "H", "V", "W", "Y", "K", "J", "X", "Q", "Z"]; | ||
|
||
var scoreTable = this.scoreTable; //this local variable is necessary to bypass the error that otherwise would be thrown in line 18 because the scope of 'this' changes inside anonymous functions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most common way to get beyond this issue is to create a variable called |
||
|
||
if (word.length > 7) { | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might also be good to return 0 when the length is zero |
||
} | ||
|
||
var wordAsArray = word.toUpperCase().split(""); | ||
wordAsArray.forEach(function(letter) { | ||
if (alphaChars.includes(letter)) { | ||
scoreTable.forEach(function(scoreRow) { | ||
if (scoreRow.includes(letter)) { | ||
wordScore += scoreRow[0]; | ||
} | ||
}); | ||
} else if (alphaChars.includes(letter) === false) { | ||
nonAlphaChar += 1; // this is my second or fourth idea how to handle words with non-alpha characters, since the forEach function behaves differently than I originally expected. | ||
} | ||
}); | ||
if (nonAlphaChar !== 0) { | ||
return 0; | ||
} else { | ||
return wordScore; | ||
} | ||
}; | ||
|
||
// highestScoreFrom(arrayOfWords): returns the word in the array with the highest score (based on specs). | ||
Scrabble.prototype.highestScoreFrom = function(arrayOfWords) { | ||
var scores = []; | ||
for (var i = 0; i < arrayOfWords.length; i++) { | ||
aScore = this.score(arrayOfWords[i]); | ||
scores.push(aScore); | ||
} | ||
|
||
// find the highest score | ||
var greatestVal = 0; | ||
for (var j = 0; j < scores.length; j++) { | ||
if (scores[j] > greatestVal) { | ||
greatestVal = scores[j]; | ||
} | ||
} | ||
|
||
// to handle "tie" situations, creating a new array with the words whose score is the greatestVal | ||
var highestScoreWords = []; | ||
for (var k = 0; k < arrayOfWords.length; k++) { | ||
if (this.score(arrayOfWords[k]) === greatestVal) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be more efficient to use the |
||
highestScoreWords.push(arrayOfWords[k]); | ||
} | ||
} | ||
|
||
// console.log(highestScoreWords); // this line above was solely for internal manual testing purposes to confirm everything below is working as expected. Could turn this back 'on' for testing in the future if doing tweaks on this object. | ||
|
||
if (highestScoreWords.length === 1) { | ||
return highestScoreWords[0]; | ||
} else { | ||
var minLength = 7; | ||
for (var l = 0; l < highestScoreWords.length; l++) { | ||
if (highestScoreWords[l].length === 7) { | ||
return highestScoreWords[l]; | ||
} else if (highestScoreWords[l].length < minLength) { | ||
minLength = highestScoreWords[l].length; | ||
} | ||
} | ||
for (var m = 0; m < highestScoreWords.length; m++) { | ||
if (highestScoreWords[m].length === minLength) { | ||
return highestScoreWords[m]; | ||
} | ||
} | ||
|
||
} | ||
}; | ||
|
||
|
||
|
||
var Player = function(name) { | ||
this.name = name.toUpperCase(); | ||
this.plays = []; | ||
this.game = new Scrabble(); | ||
}; | ||
|
||
Player.prototype.totalScore = function() { | ||
var playerScore = 0; | ||
for (var n = 0; n < this.plays.length; n++) { | ||
playerScore += this.game.score(this.plays[n]); | ||
} | ||
return playerScore; | ||
}; | ||
|
||
// YOUR CODE HERE | ||
Player.prototype.hasWon = function() { | ||
if (this.totalScore() > 100) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
Player.prototype.play = function(word) { | ||
if (this.totalScore() > 100) { | ||
return false; | ||
} else { | ||
this.plays.push(word.toUpperCase()); | ||
} | ||
}; | ||
|
||
Player.prototype.highestScoringWord = function() { | ||
return this.game.highestScoreFrom(this.plays); | ||
}; | ||
|
||
Player.prototype.highestWordScore = function() { | ||
return this.game.score(this.highestScoringWord()); | ||
}; | ||
|
||
|
||
|
||
// YOUR CODE HERE - this and the next 4 lines part of the original file | ||
Scrabble.prototype.helloWorld = function() { | ||
return 'hello world!'; | ||
}; | ||
|
||
|
||
|
||
// TESTING SCRABBLE... (see comments for expected outputs) | ||
var testingS = new Scrabble(); | ||
console.log(testingS.helloWorld()); // hello world! - this was in the original file, keeping in here for fun. | ||
console.log(testingS.score("quiz")); // 22 | ||
console.log(testingS.score("quizlets")); // 0, because the word's length is more than 7. | ||
console.log(testingS.score("quiz1")); // 0, because one character is not A-Z. | ||
console.log(testingS.score("quiz ")); // 0, because one character is not A-Z. | ||
console.log(testingS.score("quiz*")); // 0, because one character is not A-Z. | ||
console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq"])); // qqqq - when there's only one highest scoring word, this is the clear winner. | ||
console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqq", "qqqkdda"])); // qqqkdda - when multiple words have the same highest score, the (first) word in the input array that is 7-letters wins. | ||
console.log(testingS.highestScoreFrom(["tea", "coffee", "qqqkk", "zzzz", "qqqq"])); // zzzz - when multiple words have the same highest score and none of them are 7-letters, the (first) highest scoring word with the fewest letters wins. | ||
|
||
|
||
|
||
// TESTING PLAYER... (see comments for expected outputs) | ||
var testingP = new Player('suzannah'); | ||
console.log(testingP.name); // SUZANNAH | ||
testingP.play("zebras"); | ||
testingP.play("giraffe"); | ||
console.log(testingP.plays); // [ 'ZEBRA', 'GIRAFFE' ] | ||
console.log(testingP.totalScore()); // 31 | ||
console.log(testingP.hasWon()); // false | ||
testingP.play("qqqqqqq"); | ||
console.log(testingP.plays); // [ 'ZEBRAS', 'GIRAFFE', 'QQQQQQQ' ] | ||
console.log(testingP.totalScore()); // 101 | ||
console.log(testingP.hasWon()); // true | ||
console.log(testingP.play("is")); // false - because this player has already won. | ||
console.log(testingP.plays); // [ 'ZEBRAS', 'GIRAFFE', 'QQQQQQQ' ] - simply confirming that nothing was added into the plays array from the line above. | ||
console.log(testingP.highestScoringWord()); // QQQQQQQ | ||
console.log(testingP.highestWordScore()); // 70 | ||
|
||
|
||
|
||
module.exports = Scrabble; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way this is structured could use JS objects to increase clarity. Using different data at specific indexes can oftentimes be unreliable.