Basic JavaScript - Counting Cards switch with if-else doesn't works

Tell us what’s happening:
I don’t understand how to go with this problem…I have tried switch with if-else block inside cases but that isn’t working out.

Your code so far

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+=1;
      if(count>0){
        return count+'Bet';
        }
      else{
        return count+'Hold';}
      break;
    case 7:
    case 8:
    case 9:
      count+=0;
      if(count>0){
        return count+'Bet';}
      else{
        return count+'Hold';}
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count-=1;
      if(count>0){
        return count+'Bet';}
      else{
        return count+'Hold';}
      break;
  }
  

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

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

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Your code works but for the fact that it returns ‘1Bet’ or ‘-2Hold’ because you haven’t accounted for spacing in your returned string.

You could also tidy up your function a bit by only having one return statement at the end of your function, outside your switch statement, instead of having a redundant return "Change Me" statement.

Thanks , I didn’t see the spacing part.
Also would you suggest that using one if-else block after switch , I thought break in switch would lead to exit from the function …I guess it would only lead to exit from switch.

Yes, the break only breaks out of the switch statement. It doesn’t act as a function return. So you can put whatever code you like after the switch statement, before you return out of the function.

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