Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added is two pair

  • Loading branch information...
commit 992581607bb45af438387c91622c296989319c85 1 parent 23168b4
dodozhang21 authored September 25, 2011
26  WebContent/js/Game.js
@@ -81,10 +81,34 @@ Game.prototype = {
81 81
     },
82 82
     
83 83
     isTwoPair : function(sortedNumbers) {
84  
-    	return false;
  84
+    	var numberOfRepeatedElements = [];
  85
+    	for(var i in sortedNumbers) {
  86
+    		var number = sortedNumbers[i];
  87
+    		var occurrences = sortedNumbers.filter(number);
  88
+//    		console.log('number='+number+',occurrences='+occurrences);
  89
+    		numberOfRepeatedElements.push(occurrences.length);
  90
+    	}
  91
+    	// at least 4 elements have 2
  92
+    	var elementsRepeatedTwice = 0;
  93
+    	for(var i in numberOfRepeatedElements) {
  94
+    		var repeatedElement = numberOfRepeatedElements[i];
  95
+//    		console.log('repeatedElement='+repeatedElement);
  96
+    		if(repeatedElement == 2) {
  97
+    			elementsRepeatedTwice++;
  98
+    		}
  99
+    	}
  100
+//    	console.log('elementsWithTwo='+elementsRepeatedTwice);
  101
+//    	console.log(numberOfRepeatedElements);
  102
+    	return elementsRepeatedTwice >= 4;
85 103
     },
86 104
     
87 105
     isJacksOrBetter : function(sortedNumbers) {
  106
+    	for(var i in sortedNumbers) {
  107
+    		var number = sortedNumbers[i];
  108
+    		if(number >= 11) {
  109
+    			return true;
  110
+    		}
  111
+    	}
88 112
     	return false;
89 113
     },
90 114
     
13  WebContent/js/test/GameTest.js
@@ -44,6 +44,7 @@ TestCase('GameTest', {
44 44
     	var game = new Game();
45 45
     	assertTrue("Should be three of a kind.", game.isThreeOfAKind([2, 2, 4, 8, 2]));
46 46
     	assertFalse("Should not be three of a kind.", game.isThreeOfAKind([2, 2, 4, 3, 11]));
  47
+    	assertFalse("Should not be three of a kind.", game.isThreeOfAKind([2, 2, 3, 3, 2])); // <-- should be full house
47 48
     },
48 49
     
49 50
     testIsStraight : function() {
@@ -51,5 +52,17 @@ TestCase('GameTest', {
51 52
     	assertTrue("Should be a straight.", game.isStraight([2, 3, 4, 5, 6]));
52 53
     	assertTrue("Should be a straight.", game.isStraight([7, 8, 9, 10, 11]));
53 54
     	assertFalse("Should not be a straight.", game.isStraight([7, 8, 2, 10, 11]));
  55
+    },
  56
+    
  57
+    testIsJacksOrBetter : function() {
  58
+    	var game = new Game();
  59
+    	assertTrue("Should be jacks or better.", game.isJacksOrBetter([2, 3, 11, 5, 6]));
  60
+    	assertFalse("Should not be jacks or better.", game.isJacksOrBetter([7, 8, 2, 10, 3]));
  61
+    },
  62
+    
  63
+    testIsTwoPair : function() {
  64
+    	var game = new Game();
  65
+    	assertTrue("Should be two pairs.", game.isTwoPair([2, 2, 5, 5, 6]));
  66
+    	assertFalse("Should not be two pairs.", game.isTwoPair([7, 8, 2, 10, 3]));
54 67
     }
55 68
 });

0 notes on commit 9925816

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