Basic JavaScript: Counting Cards can someone help and explain this?

Tell us what’s happening:

I don’t understand this lesson, I need help understanding what is this code like what is the logic, how it’s used, and how to pass this lesson.

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 
   case 
   case 
   count --;
   break;
 }

 return "Change Me";
 // 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/81.0.4044.122 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

From what I see in your code, you’re almost there. Although you should always have a break statement inside each case statement, for this case however, it’s a trick you can use by NOT putting those break statements. It does work because I remember the way I had implemented this exercise was similar to yours.
Then at the end of your function, you will have something like this:

if (count == 0) {
   return count+ " Hold";
} else if ( count > 1) {
   return ...
   // and so on
}

So do I add break; under each case: statement?

Do I use if/else statements or switch statements?

Theoretically you should. However in this case, you can “cheat”. And by that I mean:

switch(card) {
   case 2: // all these
   case 3: // cases share
   case 4: // the same set
   case 5: // of operations.
   case 6: // Which is: increment by 1 when you get 2, 3, 4, 5 or 6.
   count ++; // here is your increment operation
   break; // and here is your break, making sure to stop only if you get one of those values above
   case 
   case 
   case 
   count --;
   break;
 }

The orthodox programmers would tell you that that piece of code looks horrible and you should never do such thing.
Okay, here is the correct alternative to that dodgy code:

switch(card) {
   case 2:
      count++;
      break;
   case 3:
      count++;
      break;
   case 4:
      count++;
      break;
   case 5:
      count++;
      break;
   case 6:
      count++;
      break;
   case  
   case 
   case 
   count--;
   break;
 }

Now tell me, which one has fewer lines and comes up with the same result? Like I said about the previous code, you should avoid NOT using break inside of a case statement because you can easily lose control of you logic and outcomes. But for such simple function, coutindCards, you can think ouside of the box a bit and do the dodgy way sometimes. So, it’s up to you to decide which way to go hahaha :wink: :+1:

This is what I put inside the comments

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;

}

Good thing is that it shows if I got them right or wrong this time, bad thing is that all of them a Xs.

I’m going to try to do it this way.

You are on the way. At the end of your switch you will have to implement what I mentioned previously here:

This is what I have inside the comments, somethings are wrong, can tell me what?

switch(card) {

case 2:

count++;

break;

case 3:

count++;

break;

case 4:

count++;

break;

case 5:

count++;

break;

case 6:

count++;

break;

case 7:

case 8:

case 9:

count--;

break;

}

if (count == 0) {

return count+ " Hold";

} else if ( count > 1) {

return …

// and so on

}

return “Change Me”;

What do you mean by //so on...?

Ok this is the solution how can I practice more with it?

switch(card) {

case 2:

count++;

break;

case 3:

count++;

break;

case 4:

count++;

break;

case 5:

count++;

break;

case 6:

count++;

break;

case 10:

case "J":

case "Q":

case "K":

case "A":

count--;

break;

}

var holdbet = ‘Hold’

if (count > 0) {

holdbet = 'Bet'

}

return count + " " + holdbet;

Thanks for helping, what programs can someone make with this?

Hey @DanAmbitious, I’ve created a new topic about your last question. If you want, please feel free to check it out:

1 Like

Thanks, I understand the card counting lesson a bit better, with an actual program.