-1

I'm struggling to understand the "return count" portion of this card counting function below:

var count = 0;

function cc(card) {
  switch (card){
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count+=1;
      break;
    case 7:
    case 8:
    case 9:
      count+=0;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count-= 1;
      break;
  }

  return count + (count > 0 ? " Bet" : " Hold");
}
cc(2); cc(3); cc(7); cc('K'); cc('A');

I know that it is there to return the count, but what is the purpose of the '?' in the statement? I thinking I understand the ":", I'm assuming it means something like "else print 'hold'.

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – Teemu Jul 14 '16 at 15:26

1 Answers1

3

It is the ternary/conditional operator. it will return:

count + "Bet" if count > 0
count + "Hold" if count <= 0 (otherwise)
Idos
  • 14,036
  • 13
  • 48
  • 65