Skip to content

Instantly share code, notes, and snippets.

@craigwendel
Created June 13, 2017 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigwendel/edae8dfe0064a17e4018aea737c18c29 to your computer and use it in GitHub Desktop.
Save craigwendel/edae8dfe0064a17e4018aea737c18c29 to your computer and use it in GitHub Desktop.
Blackjack Hand Calculator
/*
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;
}
(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