Curious about why I receive different results

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**
let 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;
}
var holdbet = "Hold"   
if (0 < count);
{holdbet = "Bet"
}

return count + " " + holdbet;
// Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
//other code

"use strict"

let count = 0;

function cc(card) {

if (card == 2, 3, 4, 5, 6) {

   count++;

    }  else if (card == 10, "J" ,"Q", "K", "A") {

  count--;

    }  

 var holdbet = "Hold"

    if (0 < count)   {

         holdbet = "Bet"}

return count + " " + holdbet;

}

cc(2); cc(3); cc(7); cc('K'); cc('A');

console.log(cc('K'))
  **Your browser information:**

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

Challenge: Counting Cards

Link to the challenge:

Multiple comparisons like the above, and your other one, as well, cannot be performed by the interpreter. If anything happens, at all, it will be a singular comparison to the first item in the sequence.

That’s what makes the switch a viable option since there won’t be a gazillion if comparisons.

Thanks a lot for the feedback!

1 Like

As @mtf pointed out, your issue seems to be with the condition part of your if statement:

if (card == 2, 3, 4, 5, 6) {

This isn’t the correct syntax to achieve what you want. The correct syntax would look like the following code snippet:

if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6) {

Also, I would recommend that you get in the habit of using the triple equals operator instead of the double equals operator:

if (card === 2 || card === 3 || card === 4 || card === 5 || card === 6) {

This is a bit verbose, so another option would be to use Array.includes:

if ([2,3,4,5,6].includes(card)) {
1 Like