Counting card challenge , switch case

Tell us what’s happening:
Even though I use 7,8,9 in the switch case and just put count in it as it is (without changing), the code is still running. But according to me, it should not pass all the test cases.
Explanation with an example:
Once I call, say, cc(2), the value count is set to 1 GLOBALLY. Right?
Now when I call cc(7), since the count is not being changed in this case, it’s value should still be 1 which is >0 and hence, our function should return “Bet” which is not the happening here.
Please help me wrapping my head around this.
Your code so far


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

var holdBet;
if(count>0){
holdBet = "Bet"
}else{
holdBet = "Hold"
}
return count + " " + holdBet;
// Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

You are right in your thoughts: calling cc(2) and cc(7) will set count to 1.
The output is also what’s expected. You can see it by simply trying the following:

[your code here]

cc(2); // first call
// we log the output of the second function call
console.log(cc(7)); // 1 Bet.

Edit.
Maybe for an easier visualization you can assign the return value from the function and log it step by step.
For example:

[your code here]

let response = cc(2);
console.log(response) // 1 Bet
response = cc(10);
console.log(response) // 0 Hold
response = cc('A');
console.log(response) // -1 Hold

As you can see the function is doing what’s expected on each step.
Hope this helps :sparkles:

2 Likes

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