# Basic JavaScript - Counting Cards

Tell us what’s happening:
I watched the video with it and I still don’t fully understand what’s going on. Especially the `var holdbet="Hold" if (count>0){ holdbet="Bet" } return count+ " "+holdbet;`
part of the code

``````let count = 0;

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;
}
var holdbet="Hold"
if (count>0){
holdbet="Bet"
}
return count+ " "+holdbet;
// Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
``````

Challenge: Basic JavaScript - Counting Cards

Can you explain a little more what you don’t understand about the above? I’m guessing you understand at least some of it. So please tell us exactly which part you don’t understand so we can concentrate on that.

In the last bit of the question it asks

“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 players decision (`Bet` or `Hold`) should be separated by a single space.”

So I know the last sentence represents `return count+ " "+holdbet;`

but I’m still confused on the if statement because the question asks “`Bet` if the count is positive, or `Hold` if the count is zero or negative.” so shouldn’t there be an if and else if statement like this

``````var holdbet="Hold"
if (count>0){
holdbet="Bet"
} else if (count<=0){
holdbet="Hold"
``````

Hopefully my question kind of makes sense.

Before the `if` statement you have this:

``````var holdbet="Hold"
``````

So this means that the variable `holdbet` contains the string “Hold”.

The `if` statement changes the value of `holdbet` to “Bet” if `count` is greater than `0`. But if it isn’t greater than `0`, then do you really need an `else if` to set `holdbet` to “Hold” if it is already set to “Hold”?

Do you see how `holdbet` starts out as “Hold” and thus we don’t need to set it to “Hold” again.

Now, if we didn’t give `holdbet` a starting value when we declared it, such as:

``````var holdbet;
``````

Then the value of `holdbet` would be `undefined` and we would need to set it to either “Bet” or “Hold” below. But giving it a default value when we declare it saves us from having to do both of those later on.

This makes a lot more sense I finally get it now thank you!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.