Card Counter, what am I doing wrong here?

Tell us what’s happening:
Describe your issue in detail here.

I’ve been attempting the card counter excercise and used switch statements for the code and it’s counting the negative and 0 count value cards right but not the positive?

  **Your code so far**

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:
  count+= 0;
  break;
  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
  count--;
  break;
}



var holdOrBet = 'Hold'
  if (count > 0){
  return holdOrBet = 'Bet'
}


  return count + ' ' + holdOrBet;
  // Only change code above this line
}

// running tests Cards Sequence 2, 3, 4, 5, 6 should return

5 Bet

Cards Sequence 2, J, 9, 2, 7 should return the string

1 Bet

Cards Sequence 2, 2, 10 should return the string

1 Bet

// tests completed

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

I don’t think this return statement is doing what you want it to do. Why use a return here?

Thanks and I guess thats just my mistake, ( in my mind I wanted the if statement to return holdOrBet =‘Bet’ when count > 0) is it because using the return statement ends the whole function there and the rest of the code after doesn’t get executed?

Yep. Return statements are for terminating the current function.

Okay, thank you so much for your help!

1 Like

Oh I have a question because if the code after the if statemement doesn’t get executed then why did the card sequences that returned a negative value or 0 ( Hold ) still went through and were correct if this part of the code didn’t execute?

 return count + ' ' + holdOrBet;

The function takes the first return statement in the code path.if the if statement was not true, then the second return is used.

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