# Counting Cards Using If Else Statements

Can Anyone Explain How The Conditions and Blocks of Codes Inside The If Else Statements Work ?

``````I understand simple if else and ternary but not this, it looks like ternary operator if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6)   //  Is it because there are 5 elements inside?

cc(2); cc(3); cc(4); cc(5); cc(6);     // It displays 5 Bet
How does this else if display "0 Hold" when I call the function with  cc(7);  cc(8);  cc(9);  ?
``````
``````var count = 0;
function cc(card) {
// Only change code below this line
if(card == 2 || card == 3 || card == 4 || card == 5 || card == 6){
count += 1;
}else if(card == 7 || card == 8 || card == 9){
count += 0;
}else if(card == 10 || card == 'J' || card == 'Q' || card == 'K' || card ==
'A'){
count -= 1;
}
if(count > 0){
return count + " Bet";
}return count + " Hold";
// Only change code above this line
}

``````

I would encourage you to simplify some of this code.

For example you’re saying “If the card is equal to 2,3,4,5,6”, you could just as easily provide some boundaries.

`if( card > 1 || card < 7)`

Next, it looks like you’re saying: “if the card is 7,8,9 then add 0”. Since you’re starting with 0, and then adding 0, your result is 0.

Lastly, you’re saying “if count is 0 then hold”. This is why `cc(7)` returns “0 Hold”

1 Like

What does that "count += " do? Does it just 1 to 0 ?

`count += \$NUMBER` is the equivalent of:

`count = count + \$NUMBER`

In other words, it takes the value of `count` and adds whatever number you specify to it.

It is a shorthand notation that makes it easier to write and read.

When I called the cc (card) function with cc(2); cc(3); it displays " 2 Bet " and when I called cc(card) function with cc(2); cc(3); cc(4); cc(5); cc(6); it displays " 5 Bet “, so in this case " 5 Bet " is equal to " count += 1 ? Meaning “1” become 5 and displays " 5 Bet” in the result?

Can you add all the code to codepen and paste a link here? It’s important to see how you are calling these functions and what the value of `count` is in various places.

The value of `count` is never reset as far as I can tell. So each time you call cc the count starts from the last known number instead of 0.

You can fix this either by making count local to the function (i.e. define it inside of the function).

Or you can add a line that reset the count to 0 when the function starts.

May I see your code example with explanation below?

Sure thing, here is your code modified to what I described.

``````function cc(card) {
var count = 0;
// Only change code below this line
if(card == 2 || card == 3 || card == 4 || card == 5 || card == 6){
count += 1;
} else if(card == 7 || card == 8 || card == 9){
count += 0;
} else if(card == 10 || card == 'J' || card == 'Q' || card == 'K' || card ==
'A'){
count -= 1;
}

if(count > 0){
return count + " Bet";
}
return count + " Hold";
// Only change code above this line
}

cc(2);
cc(3);
cc(4);
cc(5);
cc(6);
``````

My understanding is the value of count should NOT be reset.
This is meant to be a card-counting script, for blackjack. It needs to maintain the value of count based on all previous cards, not just the one being currently passed.

I should be returning `6 Bet` here.

1 Like

I see. I guess I am not understanding the OPs question then.

You have the right code.

``````if (cond1 || cond2 || cond3) {
...
}
``````

enters the `if` block if any one of the conditions is true. The `||` symbol means or.

so this count += 1; does nothing even when all condition 2, 3,4,5,6 equal cc( 2); cc(2); cc(3); cc(4); cc(5); cc(6); ?

``````var count = 0;
function cc(card) {
// Only change code below this line
if(card == 2 || card == 3 || card == 4 || card == 5 || card == 6){
count += 1;     // so this count += 1; does nothing even when all condition 2, 3,4,5,6 equal cc( 2); cc(2); cc(3); cc(4); cc(5); cc(6); ?
}else if(card == 7 || card == 8 || card == 9){
count += 0;
}else if(card == 10 || card == 'J' || card == 'Q' || card == 'K' || card ==
'A'){
count -= 1;
}
if(count > 0){
return count + " Bet";
}return count + " Hold";
// Only change code above this line
}
cc(2); cc(3); cc(4); cc(5); cc(6);
"5 Bet"
``````

The `count +1;` is what increments the count up by 1. In counting cards, the player keeps track of which cards have been dealt with a `count` variable. A high value for `count` means the player should bet, as the odds of winning are higher. See the console below to observe how the `count` variable changes as I pass each card through the function.

Hello! What are you not understanding?

1 Like

var count = 0;
function cc(card) {
// Only change code below this line
if(card == 2 || card == 3 || card == 4 || card == 5 || card == 6){
count += 1;
}else if(card == 7 || card == 8 || card == 9){
count += 0;
}else if(card == 10 || card == ‘J’ || card == ‘Q’ || card == ‘K’ || card ==
‘A’){
count -= 1;
}
if(count > 0){
return count + " Bet";
}return count + " Hold";
// Only change code above this line
}

console.log(cc(2)); // outputs 1 Bet
console.log(cc(3)); // outputs 2 Bet
console.log(cc(7)); // still outputs 2 Bet. Should this output " 0 Hold " ? or Is it because the counting jumps to card == 7 from card == 3?
console.log(cc(10)); still outputs 1 Bet. Should this output " - 1 Hold " ?
console.log(cc(‘K’)); // outputs " 0 Hold "; Should this output " - 2 Hold " ?
console.log(cc(‘Q’)); // outputs " - 1 Hold "; Should this output " - 3 Hold " ?

It outputs `2 Bet` because a card value of `7` doesn’t change the count, so the count remains the same. This is how it is supposed to work.

No, this outputs `1 Bet` because the previous count was `2`, and a card value of `10` decreases the count by 1.

1 Like

When I started calling cc(card) with console.log(cc(7)); it outputs " 0 Hold "

Because if that’s the first time you called the function, `count` started at 0 and hasn’t been changed by other cards yet.
`count` is a global variable, which is overridden by the `cc()` function each time it gets called.

1 Like