freeCodeCamp Challenge Guide: Counting Cards

Try using this, i just work through it using this more simplify switch statement.

var count = 0;

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

return “Change Me”;
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

I tried this
var count = 0;

function cc(card) {
// Only change code below this line
if (card >= 2 && card <=6){
count ++;
console.log(count + ’ Bet’);

} else if (card >= 7 && card <= 9){
console.log(count + ’ Hold’);

} else if(card == 10,‘J’,‘Q’,‘K’,‘A’){
count --;
console.log(count + ’ Hold’);

} else{

return “Change Me”;
}
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

does this code not work at all?
or do we have to use case statements?

Hey this looks fine but return does not need () with it.

Can you please see this and explain why this doesn’t work ?

I get the right result but its not concatenating the count with the text

var count = 0;

function cc(card) {

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”;
}

// Only change code below this line

return “Change Me”;
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

1 Like

That doesn’t work because case evaluates equality. Essentially, if you wrote switch (variable) and case(testCase), it would perform variable === testCase, or, in this case, card === card >= 2 && card <= 6.

It would then run the code in the case statement if card === card >= 2 && card <= 6 evaluated to True.

For what you’re trying to do, you’d need to write if (card >= 2 && card <= 6) {statement;}

AWESOME EXPLANATION!!! Thanks

When I enter the soultion into Chrome console what should I see as result? “Bet” or “Hold”? I add at the very end: “function cc(2)” (without quotes of course) but it returns only “Uncaught SyntaxError: Unexpected number” . What do I do wrong? What will be the correct way to test this code working? Thanks in advance! Yarve

2 Likes