Arbitrary code? Card Counter exercise

I FINALLY solved the card counter exercise. I spent a lot longer than I think I should have re-visiting each preceding lesson to see if I’d missed something. Although I solved it, I feel like my code might have been excessive? Wondering if someone will just review and give pointers - I’m hoping to expose any misunderstandings in code and clarify.

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++;
  if (count > 0) {
    return count + " Bet"
  }

  else if (count <= 0) {
    return count + " Hold"
  }
  break;

  case 7:
  case 8:
  case 9:
  if (count > 0) {
    return count + " Bet"
  }

  else if (count <= 0) {
    return count + " Hold"
  }
  break;

  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
  count--;
  if (count > 0) {
    return count + " Bet"
  }

  else if (count <= 0) {
    return count + " Hold"
  }
  break;
    
  
}
  // Only change code above this line
}

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

It passed the tests, but, again, I think I could have left the 7, 8, and 9 switch cases out along with the if/else statement since it doesn’t add or take away from the total count? I left it in because I didn’t want to get an undefined answer either.

you can take the if/return statements outside of the switch block, and not repeat them for every case. Let switch only determine what is to be done with the count variable and once thats done(and switch is broke), put the conditions deciding what to return from the function

2 Likes

I think I understand! The If/return statements would be part of the whole function but not part of that switch statement within the function. The code would look something like this then:

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++;
  break;

  case 7:
  case 8:
  case 9:
  break;

  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
  count--;
  break;
  }

  if (count > 0) {
    return count + " Bet"
  }

  else if (count <= 0) {
    return count + " Hold"
  }
  // Only change code above this line
}

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

I think?

That looks good. This challenge tells you to use fairly arbitrary conditions, so don’t worry about that for right now.

Thank you all so much for your help!

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