2018-09-30 23:01:58 +01:00
---
id: 565bbe00e9cc8ac0725390f4
title: Counting Cards
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/c6KE7ty'
2019-07-31 11:32:23 -07:00
forumTopicId: 16809
2021-01-13 03:31:00 +01:00
dashedName: counting-cards
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
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 remaining in the deck. This is called [Card Counting ](https://en.wikipedia.org/wiki/Card_counting ).
2018-09-30 23:01:58 +01:00
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.
2020-11-27 19:02:05 +01:00
< table class = 'table table-striped' > < thead > < tr > < th > Count Change< / th > < th > Cards< / th > < / tr > < / thead > < tbody > < tr > < td > +1< / td > < td > 2, 3, 4, 5, 6< / td > < / tr > < tr > < td > 0< / td > < td > 7, 8, 9< / td > < / tr > < tr > < td > -1< / td > < td > 10, 'J', 'Q', 'K', 'A'< / td > < / tr > < / tbody > < / table >
You will 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 (see table). 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.
2021-03-25 15:07:48 -06:00
**Example Outputs:** `-3 Hold` or `5 Bet`
2020-11-27 19:02:05 +01:00
**Hint**
Do NOT reset `count` to 0 when value is 7, 8, or 9.
Do NOT return an array.
Do NOT include quotes (single or double) in the output.
# --hints--
2021-12-08 18:32:56 +03:00
Cards Sequence 2, 3, 4, 5, 6 should return the string `5 Bet`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(2);
cc(3);
cc(4);
cc(5);
var out = cc(6);
if (out === '5 Bet') {
return true;
}
return false;
})()
);
```
2021-03-02 16:12:12 -08:00
Cards Sequence 7, 8, 9 should return the string `0 Hold`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(7);
cc(8);
var out = cc(9);
if (out === '0 Hold') {
return true;
}
return false;
})()
);
```
2021-03-02 16:12:12 -08:00
Cards Sequence 10, J, Q, K, A should return the string `-5 Hold`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(10);
cc('J');
cc('Q');
cc('K');
var out = cc('A');
if (out === '-5 Hold') {
return true;
}
return false;
})()
);
```
2021-03-02 16:12:12 -08:00
Cards Sequence 3, 7, Q, 8, A should return the string `-1 Hold`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(3);
cc(7);
cc('Q');
cc(8);
var out = cc('A');
if (out === '-1 Hold') {
return true;
}
return false;
})()
);
```
2021-03-02 16:12:12 -08:00
Cards Sequence 2, J, 9, 2, 7 should return the string `1 Bet`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(2);
cc('J');
cc(9);
cc(2);
var out = cc(7);
if (out === '1 Bet') {
return true;
}
return false;
})()
);
2018-09-30 23:01:58 +01:00
```
2021-03-02 16:12:12 -08:00
Cards Sequence 2, 2, 10 should return the string `1 Bet`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(2);
cc(2);
var out = cc(10);
if (out === '1 Bet') {
return true;
}
return false;
})()
);
```
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
Cards Sequence 3, 2, A, 10, K should return the string `-1 Hold`
2020-11-27 19:02:05 +01:00
```js
assert(
(function () {
count = 0;
cc(3);
cc(2);
cc('A');
cc(10);
var out = cc('K');
if (out === '-1 Hold') {
return true;
}
return false;
})()
);
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
let count = 0;
2018-09-30 23:01:58 +01:00
function cc(card) {
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
return "Change Me";
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2021-10-26 01:55:58 +09:00
let count = 0;
2018-09-30 23:01:58 +01:00
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--;
}
if(count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}
```