Basic JavaScript: Counting Cards
2020-12-30 08:29
标签:ble amp out pos game reset -- param UNC 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. 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. You will write a card counting function. It will receive a Example Output Hint Cards Sequence 2, 3, 4, 5, 6 should return Cards Sequence 7, 8, 9 should return Cards Sequence 10, J, Q, K, A should return Cards Sequence 3, 7, Q, 8, A should return Cards Sequence 2, J, 9, 2, 7 should return Cards Sequence 2, 2, 10 should return Cards Sequence 3, 2, A, 10, K should return Basic JavaScript: Counting Cards 标签:ble amp out pos game reset -- param UNC 原文地址:https://www.cnblogs.com/PrimerPlus/p/13022408.html
Count Change
Cards
+1
2, 3, 4, 5, 6
0
7, 8, 9
-1
10, ‘J‘, ‘Q‘, ‘K‘, ‘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.-3 Hold
5 Bet
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.Solution 1
// CountingCards.js
let count = 0;
function CountingCards(card) {
let regex = /[JQKA]/;
if (card > 1 && card 0)
return count + " Bet";
return count + " Hold";
}
console.log(CountingCards(2));
console.log(CountingCards(3));
console.log(CountingCards(7));
console.log(CountingCards(‘K‘));
console.log(CountingCards(‘A‘));
Solution 2
let count = 0;
function CountingCards(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";
}
}
console.log(CountingCards(2));
console.log(CountingCards(3));
console.log(CountingCards(7));
console.log(CountingCards(‘K‘));
console.log(CountingCards(‘A‘));
5 Bet
0 Hold
-5 Hold
-1 Hold
1 Bet
1 Bet
-1 Hold
上一篇:C/C++ const
下一篇:学习大数据:Java基础篇之方法
文章标题:Basic JavaScript: Counting Cards
文章链接:http://soscw.com/index.php/essay/39240.html