Skip to content
Browse files

implemented isTwoPair and isFullHouse

  • Loading branch information...
1 parent 9925816 commit 82a562f021fc858de5bf8764a50b01638ef02005 @dodozhang21 committed Sep 25, 2011
Showing with 16 additions and 12 deletions.
  1. +10 −12 WebContent/js/Game.js
  2. +6 −0 WebContent/js/test/GameTest.js
View
22 WebContent/js/Game.js
@@ -59,7 +59,15 @@ Game.prototype = {
},
isFullHouse : function(sortedNumbers) {
- return false;
+ var numberOfRepeatedElements = [];
+ for(var i in sortedNumbers) {
+ var number = sortedNumbers[i];
+ var occurrences = sortedNumbers.filter(number);
+ numberOfRepeatedElements.push(occurrences.length);
+ }
+
+ return numberOfRepeatedElements.filter(2).length == 2
+ && numberOfRepeatedElements.filter(3).length == 3;
},
isFlush : function(suits) {
@@ -89,17 +97,7 @@ Game.prototype = {
numberOfRepeatedElements.push(occurrences.length);
}
// at least 4 elements have 2
- var elementsRepeatedTwice = 0;
- for(var i in numberOfRepeatedElements) {
- var repeatedElement = numberOfRepeatedElements[i];
-// console.log('repeatedElement='+repeatedElement);
- if(repeatedElement == 2) {
- elementsRepeatedTwice++;
- }
- }
-// console.log('elementsWithTwo='+elementsRepeatedTwice);
-// console.log(numberOfRepeatedElements);
- return elementsRepeatedTwice >= 4;
+ return numberOfRepeatedElements.filter(2).length == 4;
},
isJacksOrBetter : function(sortedNumbers) {
View
6 WebContent/js/test/GameTest.js
@@ -64,5 +64,11 @@ TestCase('GameTest', {
var game = new Game();
assertTrue("Should be two pairs.", game.isTwoPair([2, 2, 5, 5, 6]));
assertFalse("Should not be two pairs.", game.isTwoPair([7, 8, 2, 10, 3]));
+ },
+
+ testIsFullHouse : function() {
+ var game = new Game();
+ assertTrue("Should be full house.", game.isFullHouse([2, 2, 6, 6, 6]));
+ assertFalse("Should not be full house.", game.isFullHouse([7, 8, 2, 10, 3]));
}
});

0 comments on commit 82a562f

Please sign in to comment.
Something went wrong with that request. Please try again.