Counting Cards - Question about no 4 in console.log

Hello

Why is a number (number 4 in the video) entered in the console.log statement on the last line:

var count = 0;
function cc(card) {
  switch(card){
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count++;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count --;
      break;
  }
  let holdbet = 'Hold';
    if (count > 0) {
      holdbet = 'Bet';
    }
  return count + '  ' + holdbet;
}

cc(2); cc('3'); cc(7); cc('A'); cc('K');
console.log(cc(4));

I’ve used the code on codepen and I can see the result changes if that number changes, but why is it there?

Thanks

Means your are printing whatever the function cc(4) returns. 4 is the argument to the function cc()

This code is same as below

var result = cc(4);
console.log(result);

Hi
But the result takes into account all the values entered for cc above as well. Is the number 4 just adding one more value to calculation?
This returns a result based on the values given before the last line:

console.log(cc());

so no value in the argument works fine too…
It’s just all so extremely confusing! Thank you for bothering to answer.

no argument means no card so no change to count, but the function still returns something because the return value is based on value of count

your count variable is Global, meaning it is declared outside the function cc() but you are modifying the value of it inside cc(). So each and every time you modify count inside cc(), it will keep it’s value and next time you modify it, it will just append to its previous value.

If you do not want that behavior, then move your count variable inside cc(). If you want it to be global, still this is bad coding practice as everyone have access to count and it can be modified unintentionally and you will observe strange results. In such situations it is good to use a Closure

check the challenge description, count should be global