-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting-cards-exercise.js
37 lines (33 loc) · 1.23 KB
/
counting-cards-exercise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and
// low cards remainin in the deck. This is called Card Counting. Having more high cards remaining in the deck favors the player.
// Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count
// is zero or negative, the player should bet low.
// 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
// the global 'count' variable according to the card's value. The function will then return a string with the current count and the string
// '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')
// should be separated by a single space.
var count = 0;
function cc(card) {
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');