Created
December 28, 2017 01:57
-
-
Save jlarocque/eda1fc10b67bc8ffca7b43777ac97934 to your computer and use it in GitHub Desktop.
fullstackJack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src='https://scottdalessandro.github.io/jasmine-total/js/jasmine-suite.min.js'></script> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Fullstack Jack Card Game | |
Steve and Josh are bored and want to play something. They don't want to think too much, so they come up with a simple game: | |
Write a function called **fullstackJack** and figure out who is going to win. | |
FullstackJack Rules: | |
- Each player is dealt the same number of cards | |
- They flip the card at the top of the deck | |
- The card with the highest value wins the round and the winning player receives a point | |
- The two cards used are discarded | |
- The players continue playing until there are no remaining cards | |
- The player with the most points at the end wins | |
Example | |
***Representing Cards:*** A players hand is stored in an array. Each index is a card value. The card rank is as follows (from lowest to highest):`'2','3','4','5','6','7','8','9','T','J','Q','K','A'`. Player hands are passed to the `fullstackJack` function as arguments, as in the examples below: | |
``` | |
// player1Hand = ['A', '7', '8']; | |
// player2Hand = ['K', '5', '9']; | |
fullstackJack(['A', '7', '8'], ['K', '5', '9']); | |
// --> returns "Player 1 wins 2 to 1!" | |
- Player1 is dealt ['A','7','8'] | |
- Player2 is dealt ['K','5','9'] | |
- In first round, Ace beats King and Player1 gets one point. | |
- In second round, 7 beats 5 and Player1 gets his second point. | |
- In third round, 9 beats 8 and Player2 gets one point. | |
- You should return "Player1 wins 2 to 1!" | |
``` | |
``` | |
fullstackJack(['K', 'Q', 'J'], ['Q', 'K', 'J']) | |
// --> returns "Tie!" | |
``` | |
*/ | |
// Write Code Below | |
function fullstackJack(arr1 , arr2){ | |
var cardValue = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']; | |
var score1 = 0; | |
var score2 = 0 ; | |
var message; | |
for(var i = 0; i < arr1.length; i++){ | |
if (cardValue.indexOf(arr1[i]) > cardValue.indexOf(arr2[i])){ | |
score1 ++; | |
} else if (cardValue.indexOf(arr2[i]) > cardValue.indexOf(arr1[i])){ | |
score2 ++; | |
} | |
} | |
if (score1 > score2){ | |
message = 'Player 1 wins ' + score1 + ' to ' + score2 + '!'; | |
} else if (score2 > score1){ | |
message = 'Player 2 wins ' + score2 + ' to ' + score1 + '!'; | |
} else { | |
message = 'Tie!'; | |
} | |
return message; | |
} | |
describe('fullstackJack', function(){ | |
it('stackJack is a function', function(){ | |
expect(typeof fullstackJack).toEqual('function'); | |
}); | |
it('stackJack returns a string value', function(){ | |
expect(typeof fullstackJack(["A"], ["K"]) === 'string').toEqual(true); | |
}); | |
it('stackJack indicates which player wins', function(){ | |
expect(fullstackJack(['A', '7'], ['K', '5'])).toEqual('Player 1 wins 2 to 0!'); | |
expect(fullstackJack(['K', '5'], ['A', '7'])).toEqual('Player 2 wins 2 to 0!'); | |
}); | |
it('stackJack indicates when both players "tie"', function(){ | |
expect(fullstackJack(['K', 'Q', 'J'], ['Q', 'K', 'J'])).toEqual("Tie!"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment