What's wrong with my code? Can I use || in switch case?

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

if (count > 0) {
return `${count} Bet`
} else if (count <= 0) {
return `${count} Hold`
}
// 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/98.0.4758.102 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


When you start using something in a way that has never been taught to you, you’re taking a risk that you are completely misusing the thing. In that case, you should either 1) Google and see if this is a valid way to use this or 2) experiment and see if the behavior is what you expect.

Have you Googled or experimented? What did you find out from those efforts? What happens when you run cc(10), for example? Spoiler: this is a wrong way to use a switch

1 Like

I experimented (not enough) first with || operator before (I thought it worked because freeCodeCamp accepted my solution :slight_smile: ). I’ll take your advice to google things before starting such topics, I’m just learning to learn. Thank you <3

This is the sort of code I’d run to test it:

let count = 0;

function cc(card) {
  // Only change code below this line

  switch (card) {
    case 2 || 3 || 4 || 5 || 6:
      count++;
      console.log("first case");
      break;
    case 10 || 'J' || 'Q' || 'K' || 'A':
      count--;
      console.log("second case");
      break;
  }

  if (count > 0) {
    return `${count} Bet`
  } else if (count <= 0) {
    return `${count} Hold`
  }
  // Only change code above this line
}

console.log("cc(2):");
cc(2);
console.log("count: ", count);

console.log("\ncc(3):");
cc(3);
console.log("count: ", count);

console.log("\ncc(10):");
cc(10);
console.log("count: ", count);

console.log("\ncc(11):");
cc(11);
console.log("count: ", count);

Your gut was right that something strange is happening.

1 Like

Right.

We understand what you are trying to do, but JS does not. Definitely do what Jeremy is saying. This:

2 || 3 || 4 || 5 || 6

does not do what you think it does. Do a console.log to see how JS is seeing it:

console.log(2 || 3 || 4 || 5 || 6)

There is a way to do this. Whenever I am unsure about something, I check the documentation. MDN has excellent documentation. I would google “mdn switch” and it would take me here. I would read through that. About halfway down that page, I think there will be a few paragraphs that will interest you. [hint, hint]

2 Likes

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