.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
apache
aspnet
bash
blender
blockchain
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
access-array-data-with-indexes
access-multi-dimensional-arrays-with-indexes
accessing-nested-arrays
accessing-nested-objects
accessing-object-properties-with-bracket-notation
accessing-object-properties-with-dot-notation
accessing-object-properties-with-variables
add-new-properties-to-a-javascript-object
add-two-numbers-with-javascript
adding-a-default-option-in-switch-statements
appending-variables-to-strings
assignment-with-a-returned-value
build-javascript-objects
chaining-if-else-statements
comment-your-javascript-code
comparison-with-the-equality-operator
comparison-with-the-greater-than-operator
comparison-with-the-greater-than-or-equal-to-operator
comparison-with-the-inequality-operator
comparison-with-the-less-than-operator
comparison-with-the-less-than-or-equal-to-operator
comparison-with-the-strict-equality-operator
comparison-with-the-strict-inequality-operator
comparisons-with-the-logical-and-operator
comparisons-with-the-logical-or-operator
compound-assignment-with-augmented-addition
compound-assignment-with-augmented-division
compound-assignment-with-augmented-multiplication
compound-assignment-with-augmented-subtraction
concatenating-strings-with-plus-operator
concatenating-strings-with-the-plus-equals-operator
constructing-strings-with-variables
count-backwards-with-a-for-loop
counting-cards
index.md
create-decimal-numbers-with-javascript
declare-javascript-variables
declare-string-variables
decrement-a-number-with-javascript
delete-properties-from-a-javascript-object
divide-one-decimal-by-another-with-javascript
divide-one-number-by-another-with-javascript
escape-sequences-in-strings
escaping-literal-quotes-in-strings
find-the-length-of-a-string
finding-a-remainder-in-javascript
generate-random-fractions-with-javascript
generate-random-whole-numbers-with-javascript
generate-random-whole-numbers-within-a-range
global-scope-and-functions
global-vs.-local-scope-in-functions
golf-code
increment-a-number-with-javascript
initializing-variables-with-the-assignment-operator
introducing-else-if-statements
introducing-else-statements
iterate-odd-numbers-with-a-for-loop
iterate-through-an-array-with-a-for-loop
iterate-with-javascript-do...while-loops
iterate-with-javascript-for-loops
iterate-with-javascript-while-loops
local-scope-and-functions
logical-order-in-if-else-statements
manipulate-arrays-with-pop
manipulate-arrays-with-push
manipulate-arrays-with-shift
manipulate-arrays-with-unshift
manipulating-complex-objects
modify-array-data-with-indexes
multiple-identical-options-in-switch-statements
multiply-two-decimals-with-javascript
multiply-two-numbers-with-javascript
nest-one-array-within-another-array
nesting-for-loops
passing-values-to-functions-with-arguments
practice-comparing-different-values
profile-lookup
quoting-strings-with-single-quotes
record-collection
replacing-if-else-chains-with-switch
return-a-value-from-a-function-with-return
return-early-pattern-for-functions
returning-boolean-values-from-functions
selecting-from-many-options-with-switch-statements
shopping-list
stand-in-line
store-multiple-values-in-one-variable-using-javascript-arrays
storing-values-with-the-assignment-operator
subtract-one-number-from-another-with-javascript
testing-objects-for-properties
understand-string-immutability
understanding-boolean-values
understanding-case-sensitivity-in-variables
understanding-undefined-value-returned-from-a-function
understanding-uninitialized-variables
updating-object-properties
use-bracket-notation-to-find-the-first-character-in-a-string
use-bracket-notation-to-find-the-last-character-in-a-string
use-bracket-notation-to-find-the-nth-character-in-a-string
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string
use-conditional-logic-with-if-statements
use-multiple-conditional-ternary-operators
use-the-conditional-ternary-operator
use-the-parseint-function
use-the-parseint-function-with-a-radix
using-objects-for-lookups
word-blanks
write-reusable-javascript-with-functions
index.md
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
codeigniter
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
dns
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
graphql
groovy
haskell
haxe
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mariadb
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
optical-alignment
php
powershell
product-design
progressive-web-apps
puppet
python
r
raspberry-pi
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
tools
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.french.md
README.italian.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
135 lines
6.3 KiB
Markdown
135 lines
6.3 KiB
Markdown
![]() |
---
|
||
|
title: Counting Cards
|
||
|
---
|
||
|
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||
|
|
||
|
###  Problem Explanation:
|
||
|
|
||
|
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 line` and up to `// Only change code above this line`
|
||
|
* Ensure that you are editing the inside of the `cc` function.
|
||
|
* 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
|
||
|
|
||
|
|
||
|
##  Hint: 1
|
||
|
|
||
|
Use a `switch` (or `else if`) statement to count the value of each card.
|
||
|
|
||
|
> _try to solve the problem now_
|
||
|
|
||
|
##  Hint: 2
|
||
|
|
||
|
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_
|
||
|
|
||
|
##  Hint: 3
|
||
|
|
||
|
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_
|
||
|
|
||
|
## Spoiler Alert!
|
||
|
|
||
|

|
||
|
|
||
|
**Solution ahead!**
|
||
|
|
||
|
##  Basic Code Solution:
|
||
|
```javascript
|
||
|
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
|
||
|
}
|
||
|
```
|
||
|
### Code Explanation:
|
||
|
|
||
|
* Check the value of each card via a `switch` statement.
|
||
|
* 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 `switch` statement.
|
||
|
* 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 `switch` statement hits `case 2`, jumps down and adds 1 to the variable `count`.
|
||
|
* The `switch` statement then hits the `break` and `cc(3);` runs.
|
||
|
* This cycle continues until the final call is made, `cc('A');`.
|
||
|
* After the `switch` statement, the `if` statement checks `count`, which is now 0.
|
||
|
* This then drops down to the `else` statement, which will return **0 Hold**.
|
||
|
|
||
|
**_Note_**: As mentioned earlier, the `switch` statement could have also been an `else if` statement.
|
||
|
|
||
|
|
||
|
## Additional code solution:
|
||
|
|
||
|
```javascript
|
||
|
function cc(card) {
|
||
|
// Only change code below this line
|
||
|
var regex = /[JQKA]/;
|
||
|
if (card > 1 && card < 7){count++;}
|
||
|
else if (card === 10 || String(card).match(regex)){count--;}
|
||
|
|
||
|
if (count > 0) return count + " Bet";
|
||
|
return count + " Hold";
|
||
|
|
||
|
// Only change code above this line
|
||
|
}
|
||
|
```
|
||
|
· Run code at [repl.it](https://repl.it/@AdrianSkar/Basic-JS-Counting-cards).
|
||
|
|
||
|
### Code explanation
|
||
|
· The function first evaluates `if` the condition `card` is a value greater than `1` and lower than `7`, in which case it increments `count` by one.
|
||
|
· Then if the card is `10` or higher it decrements `count` by one.
|
||
|
· The variable `regex` is a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) representing values (letters) for the higher cards.
|
||
|
· The `else` statement checks those values with the `|| (logical OR)` operator; first for `10` and then for any string that matches the regular expression using [String.match()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match).
|
||
|
|
||
|
#### Resources
|
||
|
|
||
|
* <a href='https://en.wikipedia.org/wiki/Card_counting' target='_blank' rel='nofollow'>Card counting at Wikipedia</a>
|
||
|
* <a href='http://www.freecodecamp.com/challenges/selecting-from-many-options-with-switch-statements' target='_blank' rel='nofollow'>Challenge: Selecting from many options with Switch Statements</a>
|
||
|
* <a href='http://www.freecodecamp.com/challenges/chaining-if-else-statements' target='_blank' rel='nofollow'>Challenge: Chaining If Else Statements</a>
|
||
|
* <a href='http://www.freecodecamp.com/challenges/increment-a-number-with-javascript' target='_blank' rel='nofollow'>Challenge: Increment a Number with Javascript</a>
|