# Card Counting ! (in javascript)

Card Counting ! (in javascript)
0

#1

I feel like I’m hopeless at javascript. As soon as I’m ‘sort-of’ understanding a topic we move past it in the curriculum. I can’t pass the Card Counting exercise. Here’s my code:

var count = 0;

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

// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

Here’s the link to the exercise:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/

Why do I fail!?

#2

You need to think about when you should be using the return statement. As soon as a return statement executes, the function exits.

Also, is any of the above needed?

#3

I tried this and I get the same result. all tests failed. I’m not understanding. I think I’m just going to skip this challenge and come back to it.

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"
}
return "Change Me";
// Only change code above this line
}

#4

I’m banging my head against the wall and getting nowhere. I’m going to skip for now and come back to it when I understand JS better.

#5

When you can not figure out why the tests are not accepting the values your function is returning, you should check what your function is returning. To do that in this challenge, you can add a console.log statement before each return which will have the same value which is getting returned. Below I added those console.log statements. Run the tests and then view your browser’s console (Ctrl+Shft+J in Chrome) to see what is going on for each test.

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) {
console.log(count + "Bet");
return count + "Bet"
} else {
console.log(count + "Hold");
return count + "Hold"
}
return "Change Me";
// Only change code above this line
}

I think you will figure out quickly what the issue is with your return statement.

#6

Okay, I’ll try that.