Explain the Logic! Counting Cards Challenge Displays 1 Output. Why?

Explain the Logic! Counting Cards Challenge Displays 1 Output. Why?
0

#1

Here’s the link to the challenge: https://www.freecodecamp.com/challenges/counting-cards

When calling the function multiple times: cc(2); cc(3); cc(7); cc('K'); cc('A');

why do we receive one output only, such as “-3 Hold” as opposed to getting multiple outputs per call? Do all of the arguments get executed before moving onto the if/else statement?

Also, how is the counter affected by the function in general? I understand that var count is a global variable and can be accessed from inside the function. I found it interesting that - for experimenting purposes - when I put var count inside of the function, the outputs are different, and of course, the test did not pass.

If you could explain the logic behind it all, it would be much appreciated!

Here’s my code, too, if it helps! Thank you

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;
  }
  
  if (count > 0) {
    return count + " Bet";
    
  } else if ( count <= 0) { 
     return count + " Hold";
  }
  
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

#3

Thank you for your thorough and understandable response along with your example with console log. It makes sense now :slight_smile: