-
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
Jessica's JS Scrabble #24
base: master
Are you sure you want to change the base?
Changes from all commits
f615245
249071f
a98a479
12c2f4d
aace640
6e99861
c65f9c3
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,7 +1,117 @@ | ||
var Scrabble = function() {}; | ||
// 'use strict'; | ||
// var prompt = require('prompt'); | ||
// | ||
// prompt.start(); | ||
|
||
// var Scrabble = prompt.get(['word'], function(err, result) { | ||
// this._word = result.word.toUpperCase(); | ||
// }); | ||
|
||
var letters = { | ||
"AEIOULNRST": 1, | ||
"DG": 2, | ||
"BCMP": 3, | ||
"FHVWY": 4, | ||
"K": 5, | ||
"JX": 8, | ||
"QZ": 10 | ||
}; | ||
|
||
var Scrabble = { //is it possible to initialize this as a constructor? function() | ||
|
||
}; | ||
|
||
Scrabble.score = function(entry) { | ||
var total = 0; | ||
var word = entry.toUpperCase(); | ||
if (word.length == 7) { | ||
total = 50; | ||
} | ||
for (var i = 0; i < word.length; i++) { | ||
for (var group in letters) { | ||
if (group.includes(word[i])) { | ||
total += letters[group]; | ||
} | ||
} | ||
} | ||
return total; | ||
}; | ||
|
||
Scrabble.highestScoreFrom = function(arrayOfWords) { | ||
var bestScore = 0; | ||
var bestWord = ""; | ||
arrayOfWords.forEach(function(word) { | ||
if (Scrabble.score(word) > bestScore) { | ||
bestScore = Scrabble.score(word); | ||
bestWord = word; | ||
} else if (Scrabble.score(word) == bestScore) { | ||
if (bestWord.length > word.length && bestWord.length != 7) { | ||
bestWord = word; | ||
} else if (word.length > bestWord.length && word.length == 7) { | ||
bestWord = word; | ||
} | ||
} | ||
}); | ||
return bestWord; | ||
}; | ||
|
||
var Player = function(name) { | ||
this._name = name; | ||
this.plays = []; | ||
}; | ||
|
||
Player.prototype = Scrabble; //I thought this would make the Player object inherit form Scrabble, but it basically does nothing. If I take it out, everything works the same... | ||
|
||
Player.prototype.play = function(word) { | ||
if (this.hasWon() === true) { | ||
return false; | ||
} | ||
this.plays.push(word); | ||
}; | ||
|
||
Player.prototype.totalScore = function() { | ||
var totalScore = 0; | ||
this.plays.forEach(function(word){ | ||
totalScore += Scrabble.score(word); | ||
}); | ||
return totalScore; | ||
}; | ||
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. Just FYI, the forEach function above is depreciated, but it will still work fine. |
||
|
||
Player.prototype.hasWon = function() { | ||
if (this.totalScore() >= 100) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
Player.prototype.highestScoringWord = function() { | ||
return Scrabble.highestScoreFrom(this.plays); | ||
}; | ||
|
||
Player.prototype.highestWordScore = function() { | ||
return Scrabble.score(this.highestScoringWord()); | ||
}; | ||
|
||
jess = new Player("Jessica"); | ||
jess.play("hi"); | ||
jess.play("another"); | ||
console.log(jess); | ||
console.log(jess.totalScore()); | ||
console.log(jess.hasWon()); | ||
console.log(jess.highestScoringWord()); | ||
// console.log(jess.highestWordScore()); | ||
|
||
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. Good work putting code to test things. |
||
|
||
console.log(Scrabble.score("hi")); //5 | ||
console.log(Scrabble.score("elevens")); //60 | ||
console.log(Scrabble.highestScoreFrom(["hello", "blah", "test", "elevens"])); //should return "elevens" | ||
console.log(Scrabble.highestScoreFrom(["hi", "dig"])); //should return "hi" | ||
console.log(Scrabble.highestScoreFrom(["lost", "tons"])); //should return "lost" | ||
console.log(Scrabble.highestScoreFrom(["AEIOULD", "QZQZQJ"])); //should return "AEIOULD" | ||
|
||
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. You should have a few more tests to test the different possibilities for Testing in all different orders with: |
||
// YOUR CODE HERE | ||
Scrabble.prototype.helloWorld = function() { | ||
Scrabble.helloWorld = function() { | ||
return 'hello world!'; | ||
}; | ||
|
||
|
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.
I think this series of if/else could be simplified. Think about it a little.