Skip to content

Commit 3b7e0d4

Browse files
card counting exercise
1 parent 6eda501 commit 3b7e0d4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

counting-cards-exercise.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and
2+
// low cards remainin in the deck. This is called Card Counting. Having more high cards remaining in the deck favors the player.
3+
// Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count
4+
// is zero or negative, the player should bet low.
5+
6+
// Let's write a card counting function. It will receive a 'card' parameter, which can be a number or a string, and increment or decrement
7+
// the global 'count' variable according to the card's value. The function will then return a string with the current count and the string
8+
// 'Bet' if the count is positive, or 'Hold' if the count is zero or negative. The current count and the player's decision ('Bet' or 'Hold')
9+
// should be separated by a single space.
10+
11+
var count = 0;
12+
13+
function cc(card) {
14+
switch (card) {
15+
case 2:
16+
case 3:
17+
case 4:
18+
case 5:
19+
case 6:
20+
count++;
21+
break;
22+
case 10:
23+
case "J":
24+
case "Q":
25+
case "K":
26+
case "A":
27+
count--;
28+
break;
29+
}
30+
if (count > 0) {
31+
return count + " Bet";
32+
} else {
33+
return count + " Hold";
34+
}
35+
}
36+
37+
cc(2); cc(3); cc(7); cc('K'); cc('A');

0 commit comments

Comments
 (0)