Counting Cards - question on switch function

Tell us what’s happening:
Hello,
Question about switch:
Is switch actually operating as a loop?
I was surprised seeing the solution beause with regard this code below, I was expecting the following;
1 iteration: cc(2) => count = 1 and then the function return “1 Bet”
2 iteration: cc(3) => count = 2 and then the function return “2 Bet”
etc…
here it seems that the function return in one time the total count and Bet or Hold.
Sor fo an example with the following:
cc(2); cc(3); cc(4); cc(5); cc(6);

It returns once “5 Bet”.
So it seems to me that switch is acting like a loop where all the followings
cc(2); cc(3); cc(4); cc(5); cc(6);
are called in the function before entering the IF condition.

or does it return 5 times:
1 Bet
2 Bet
3 Bet
4 Bet
5 Bet
and the exercise just accept this as a solution?

Thanks for your precision.

Your code so far
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;
}

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

cc(2); cc(3); cc(4); cc(5); cc(6);


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 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count--;
      break;
  }

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:
unable to post link

count is a global variable, each time the function is called the variable is changed. The switch statement is not a loop, but it executes five times, once for every function call

So when you have cc(2); cc(3); cc(4); cc(5); cc(6); the function is called the first time and count becomes 1, second time and it becomes 2 etc till the fifth time where it becomes 5, so the fifth function call returns 5 Bet

It is not that the exercise accept that as solution, that is the solution

Ok so I misunderstood the exercise.
I was looking for a solution that could return the result of the five called function in one time but whithout using a loop.

Now I understand.

Thank you for your reply :slight_smile:

Sorry, I meant to say

count is a global variable

I hope that didn’t confuse things too much

No it’s fine, thanks again !