Basic JavaScript - Counting Cards

let 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 ++;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      return count --;
  }
  if (count > 0) {
    return count + " Bet"; 
  } else {
    return count + " Hold"; 
  }
  // Only change code above this line
}

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

I’m redoing this challenge on my own after looking up the solution just for the sake of learning. My code passes some tests but not all of them. Could someone help me understand what I’m doing wrong here?

Thanks in advance.

1 Like

The return statement stops the execution of a function and returns a value. The switch statement shouldn’t have the return statements within itself. The last if-else statement checks the validity of the value and then returns the corresponding result.

1 Like

I removed the return statements from the switch statement. Now the code is passing different tests but not all of them. Is there anything I should change on the if else statement?

This is what the code looks like now:

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 ++;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count --;
  }
  if (count > 0) {
    return count + " Bet"; 
  } else {
    return count + " Hold"; 
  }
  // Only change code above this line
}

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

You should include break commands in your switch statement. Your code will work if you include them.

2 Likes

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