 # freeCodeCamp Challenge Guide: Counting Cards

Hello guys I would like to know the difference between:

if (count > 0) {return count + " Bet";} else {return count + " Hold";}

and

if (count > 0) {console.log(count + " Bet");} else {console.log(count + " Hold");}

fyi the former pass the test and the latter didn’t.

It also works with
count += 1
and
count -= 1

1 Like

Thanks a lot. It helped.

My initial solution is nearly identical to the example (I was shocked!). It passes all of the tests when I enter the numbers, but shows straight fails when I run it. Why?

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 + " Hold";
} else {
return count + " Bet";
}

1 Like

Ok so please tell me…I decided to do my cases like this

case (2||3||4||5||):

Now, it ran and worked with (7||8||9) but almost nothing else. Why?

This was my code that worked:

``````
function cc(card) {
// Only change code below this line

if (card >= 2 && card <= 6){
count++;
} else if (card == 10 || typeof card === 'string'){
count--;
}

if(count > 0){
return(count+" Bet");
}else{
return(count+" Hold");
}

// Only change code above this line
}

``````
7 Likes

Nice guideline, was able to solve this issue, with switch

1 Like

I think you just have bet and hold backwards, this is otherwise identical to what I used as well.

``````if (count < 1) {
return count + " Hold";
}
else return count + " Bet";
``````

From what I could understand trying to get cases to work, it seems comparisons aren’t possible?

``````  case ( >=2 && <=7) {count ++;break;}
``````

I couldn’t get code like above to work, only with:

``````case 2: case 3: case 4: etc.
``````

Is it possible to use those comparisons, or is that why people are finding if/else if easier?

1 Like

This are more elegant solution, but for novice like me I did not know I can only compare “typeof” mentioned by shikkaba

so here is my solution

``````if (card <=6){
count++;
}
else if (card>=7 && card<=9){
}
else {
count--;
}

if(count>0){
return count + " Bet";
}
else {
return count + " Hold";
}
``````

above code will give garbage result if card input is less than 2 or greater than 9 but it passed the test.

1 Like

or you can omit the (card >= 7) because above it there’s (card <= 6) already. so you code will be like this var count = 0;

function cc(card) {

if (card <= 6) ++count;
else if (card <= 9); // do nothing to count if card is either 7, 8 or 9
else --count; // all the other cases is either 10, “J”, “Q”, “K”, “A”

if (count > 0){
return count + " Bet";
} else {
return count + " Hold";
}
}

2 Likes

why does this loop over after it checks the first value? shouldn’t it break out of the loop after it hits the first case it matches and proceed to check the value of count in the if statement?

2 Likes

Can someone explain why this doesn’t work?

var count = 0;

function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count = count++ + " Bet";
break;
case 7:
case 8:
case 9:
count = 0 + " Hold";
break;
case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count = count-- + " Hold";
break;
}

return count;

I want to second this question. I had something similar and I wonder if anyone knows why doesn’t it work:

``````function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += +1;
console.log = (count + " Bet");
break;
case 7:
case 8:
case 9:
count += 0;
console.log = (count + " Hold");
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= -1;
console.log = (count + " Hold");
break;``````

Could someone please explain why this code isn’t passing? All of the cases pass except for “3,7,Q,K,A” and I’m so confused

``````    case 2,3,4,5,6:
return "5 Bet";
case 7,8,9:
return"0 Hold";
case 10,"J","Q","K","A":
return "-5 Hold";
case 3,7,"Q","K","A":
return "-1 Hold";
case 2,"J",9,2,7:
return"1 Bet";
case 2,2,10:
return "1 Bet";
case 3,2,"A",10,"K":
return "-1 Hold";

}
`````` Took me so long, but i did it! Click on blur [spoiler]var 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 7:
case 8:
case 9:
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
count–;
break;
}
return count + " Bet";
}else
return count + " Hold";
// Only change code above this line

// Only change code above this line
}

// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);
[/spoiler]

Well, I did the same mistake at the start Try to read above Hint 1, Hint 2 and Hint 3. So first Hint 1 we need to count the value of each card, for example:

switch(val){ //val is just an example
case X:
case Y:
case Z:
}

Than you need to add/sub (just like it says on Hint 2) to variable count(in ex.num), for example adding(+):

case X:
case Y:
case Z:
num++;
break;

Or Ex for Sub(-)

case X:
case Y:
case Z:
num–;
break;

When you finish with each of the cards, than we need to use the Hint 3. So for example:

if(num>0){
return num + " hello"
}else{
return num + " Bye"

Let me know if it helps .

I tried running your code and I think I know Why. I did almost the same way, but just in case added 7, 8, 9 (even though it’s says extra). Try adding cards 7, 8, 9 so it will be like this:

var count = 0;
function cc(card) {
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
count–;
break;
}
if (count>0){
return count + " Bet";
}else
return count + " Hold";
}

2 Likes

looks like you have two mistakes with :

count += +1; // should be count++; or count–; or just count;
console.log = (count + " Bet");// you should declare in a different if else statement.
break;

I tried to keep it reasonably concise.

Firstly, there’s only two circumstances in which we change the count state - if it’s a low card or a high card. We can safely ignore mid-range cards completely.

Secondly, since there are only two outcomes (“Bet” or “Hold”), we can use a ternary operator. If you’ve not used these, you have a condition e.g. `("a" == 1)` a question mark `?`, then the two outcomes separated by a colon `:`. So, `(condition) ? outcomeOne : outcomeTwo`

``````var count = 0;

function cc(card) {
// Only change code below this line
var myBet;

// We don't need a conditional for the mid-range cards: they don't affect count state.
// If cards are low, we need to increment the count.
if (card == 2 || card ==  3 || card ==  4 || card ==  5 || card ==  6) count++;
// Else if the cards are high, we need to decrement.
else if (card == 10 || card == 'J'|| card ==  'Q'|| card ==  'K'|| card ==  'A') count--;

// Finally, a ternary operator. Since there is only two variations (bet or hold) we don't need more outcomes.
(count > 0) ? myBet = count + " Bet" : myBet = count + " Hold";

return myBet;
// Only change code above this line
}