Created
June 13, 2017 20:57
-
-
Save craigwendel/edae8dfe0064a17e4018aea737c18c29 to your computer and use it in GitHub Desktop.
Blackjack Hand Calculator
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
/* | |
Implement a Blackjack hand value calculator. | |
Open up the `index.html` file and your console | |
to watch the assertions pass as you write your code. | |
Also remember, that the parameter `hand` will be an array, so | |
you'll need to parse through that first before you can start to | |
write your logic. | |
*/ | |
function handValue (hand) { | |
for (var i = 0; i < hand.length; i++) { | |
if (hand[i] == 'J' || hand[i] == 'Q' || hand[i] == 'K') { | |
hand[i] = 10; | |
} else if (hand[i] == 'A') { | |
hand[i] = 11; | |
} else { | |
hand[i] = parseInt(hand[i]); | |
} | |
console.log(hand[i]); | |
} | |
function sumOfCards (array) { | |
let sum = 0; | |
for (var i = 0; i < array.length; i++) { | |
sum = sum + array[i]; | |
} | |
return sum; | |
} | |
let handSum = 0; | |
for (var i = 0; i < hand.length; i++) { | |
handSum = sumOfCards(hand); | |
if (handSum > 21 && hand[i] == 11) { | |
hand[i] = 1; | |
} | |
} | |
handSum = sumOfCards(hand); | |
return handSum; | |
} |
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
(function () { | |
'use strict'; | |
// Assert Function | |
function assert(hand, value) { | |
let output = handValue(hand); | |
console.assert(output === value, `Expecting ${output} to be ${value}`); | |
} | |
// Blackjack Hand Tests | |
// Hand, Value | |
assert(["2", "2", "8"], 12); | |
assert(["2", "2", "K"], 14); | |
assert(["2", "Q"], 12); | |
assert(["7", "J"], 17); | |
assert(["7", "A"], 18); | |
assert(["8", "J", "A"], 19); | |
assert(["8", "A", "J"], 19); | |
assert(["8", "7", "A", "A"], 17); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment