-
Notifications
You must be signed in to change notification settings - Fork 2
Challenge Counting Cards
🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program 👥 and write your own code 📝
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.
| Value | Cards |
|---|---|
| +1 | 2, 3, 4, 5, 6 |
| 0 | 7, 8, 9 |
| -1 | 10, 'J', 'Q', 'K','A' |
You will write a card counting function. It will receive a card parameter 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.
- Change the code below
// Only change code below this lineand up to// Only change code above this line - Ensure that you are editing the inside of the
ccfunction. - Use what you've learned to check the value of each card parameter passed into the function.
- Keep a running count of that number.
- If the final count is 1 or greater, return # Hold.
- If the final count is 0 or less, return # Bet.
Example Output:
- -3 Hold
- 5 Bet
- Card counting
- Challenge: Selecting from many options with Switch Statements
- Challenge: Chaining If Else Statements
- Challenge: Increment a Number with Javascript
Use a switch (or else if) statement to count the value of each card.
try to solve the problem now
Add/subtract the value of each card to variable count. If the card is worth 0, don't do anything.
try to solve the problem now
After you've counted the cards, use an if statement to check the value of count. Also, make sure your return has a space between the number and the string.
try to solve the problem now
Solution ahead!
function cc(card) {
// Only change code below this line
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";
}
// Only change code above this line
}- Check the value of each card via a
switchstatement. - The variable count:
- Increases by 1 if the card is a 2, 3, 4, 5, or 6.
- Since 7, 8, and 9 aren't worth anything, we ignore those cards in our
switchstatement. - Decreases by 1 if the card is a 10, 'J', 'Q', 'K', or 'A'.
- Check the value of count and return the appropriate response.
Example Run
-
cc(2);runs. - The
switchstatement hitscase 2, jumps down and adds 1 to the variablecount. - The
switchstatement then hits thebreakandcc(3);runs. - This cycle continues until the final call is made,
cc('A');. - After the
switchstatement, theifstatement checkscount, which is now 0. - This then drops down to the
elsestatement, which will return 0 Hold.
Note: As mentioned earlier, the switch statement could have also been an else if statement.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @leftynaut for your help with Checkpoint: Counting Cards
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Templatefor reference.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links