Basic JavaScript - Counting Cards

Can somebody help me to fix this? I am asked to write a card counting function. But I do not see how to do it using “If” and “Else If” statements. I tried with the “switch” statement but it solves only the three or four first parts of the conditions. Can anyone follow the link and try to fix it for me? All that I saw in the forum here does not work.

let count = 0;

function cc(card) {
  // Only change code below this line
switch (card) {
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    return "5 Bet";
    break;
  case 7:
  case 8:
  case 9:
    return "0 Hold";
    break;
  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
    return "-5 Hold";
    break;
  case 3:
  case 7:
  case 'Q':
  case 8:
  case 'A':
    break;
    return "-1 Hold";
  case 2:
  case 'J':
  case 9:
  case 2:
  case 7:
    break;
    return "1 Bet";
}

  // 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/108.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

The number in that should change, depending on the running count. It won’t always be “5”. Most of the time it won’t.

Really, your switch should handle how to update the running count. Then, after the switch you have logic to figure out what to return.

I do not get what you mean. When I write it like this it works for the first part of the code you are seeing there but after
return “-5 Hold”;
break;
all that follows is incorrect. What can I do?

No, all of your return statements are wrong.

The return statements should not be part of the switch

You really cannot tell me that all I did is wrong my dear Jeremy. I ran it after writing it and part of the code was passing. In any case do you have something to propose?

I really can tell you it is wrong. It really is wrong. You passing a test is accidental with this code.

My proposal is to remove the return statements from the switch, like I said.

The switch should only update the value of count. The return value only depends upon the updated value of count. It has nothing to do with card.

The return statements come after the switch

Let me try it and see. In case you want to see the complete challengein which I am stuck I give you the link to it here.

We are familiar with the challenge. We are respectfully trying to tell you that you don’t understand what is supposed to be done.

You need a switch statement to determine how to update the count. Then, after that, you need some logic to determine what to return. (In theory you could put the return logic in the switch but then you’d have to duplicate it for each case.) What happens inside the switch is separate from what you return - they are independent - any case could end up in any return.

Perhaps you could review how card counting works. If you aren’t familiar with the concept, it will be difficult. Sometimes in coding we have to do that - do a little research to understand the problem.

1 Like

I started to get the point. Yet, as you said I need to do some researches. I’m going for it and if I do not find my way please be patient with me. I’m a big beginner. This is the first time for me to see all this . So on most cases I’m lost. Yet, I promise to work on how to fix it.

Learning JavaScript definately is harder and takes more time than learning HTML and CSS.

You are methodically true when you say that JavaScript is madder than CSS and HTML. With this I say that if others know it why can’t I do that too?

People know how to do backflips and know how to do heart surgery and know how to read Russian. I don’t know how to do any of those things. But if I worked at it I can learn.

Don’t assume that this is easy for everyone. It is hard. Everyone struggles with something - not always the same thing, but something, often many things. This work needs a lot of learning - that’s why it pays well.

Your confusion and struggles are very normal. Just keep at it, you’ll get it.

1 Like

You are meticulously all right. But I’ve always said if others know it so I can do it to. And till now I got it fine. I could fix all the obstacles that I met on my way to learning the code. And this one will not be the first to drift me aside or backward.
:sweat_smile:

Sure, if you put in the work.

Hi sir. I got stumped on this for more than half an hour too, but I figured it out. My question however, is there a much efficient way to write the cases? The reason I got stuck is because I kept trying something like this at first:

case card == (10 || "J" || "Q" || "K" || "A"):

Would this have worked? If it does, then I might’ve made some mistakes on the next lines. But I really just wanna know if this would work, for future references.

That won’t work as written but you can do something similar

Hi Jeremy! I can see that whenever you intervene in this post you just show that what is done can’t work. But what about you proposing a way to fix things? You did this code, didn’t you? You have the right answer, haven’t you? You can just share it for others to learn from you, can’t you? But instead you just keep showing that this is wrong and this too is not the good way to do things. :smile:

Jeremey is just trying to prod you in the right direction. We don’t want to give answers, we want to help people find the answers.

You have the right answer, haven’t you?

There is more than one answer. And Jeremey is a talented coder. No offense, but for an experienced coder, this is an easy problem. I don’t mean that as an insult, for both Jeremy and me, at one point this would have been difficult. That’s how you learn.

You can just share it for others to learn from you, can’t you?

Yes, we can. But I don’t think we should. If all you want is a working solution, you can probably just google one. But you will take at least twice as long to learn to code that way.

Coding is not memorizing a bunch of solutions. It’s learning to analyze and research. That’s what I do all day. Very rarely is there an answer I can google and cut and paste. Even if I do, I usually end up reworking it and making sure I understand it.

We know it’s frustrating. This is hard stuff. That’s why it pays well. Just keep at it.

Hi Kevin! Have you got a nice night? It is clear that you like to be in freeCodeCamp helping. I’m grateful for that :relieved:.

You said : “Coding is not memorizing a bunch of solutions. It’s learning to analyze and research. That’s what I do all day. Very rarely is there an answer I can google and cut and paste. Even if I do, I usually end up reworking it and making sure I understand it.” I disagree with you respectfully in at least one part of what you uttered here. This is the reason why. At first we are taught a language from scratch and each word and grammatical rule is explained to us without being able to be taken from another source. It is only when you matured at least one language that you can rely on that one to learn a new one. I’m a language teacher. I speak fluently 4 languages and I am learning others now. So let it be easy for you to grasp why I take my illustration from that field. And let this show you that I’m all but now a lazy boy who wants to memorize a bunch of solutions and get a certificate. I really love digging and seeking and :thinking:. But what do I mean? “Coding is like learning a language.” This I got it from Melinda French Gates the wife of the giant in informatics Bill Gates. So if coding is like learning a language you should agree that there is a time when one has to be taken by hand and led through all the steps to a certain point. I love learning alone. And as for researches I can tell you that I boast to be among the most lovers of that field. Indeed I love seeking and I often find. Still there is a time when you need to receive help and it should be given you for you to give it back when you succeed to pass forward.

Hopefully, I say to myself that I haven’t been too harsh in my reply. So just move out to us and let us reach where you are. Then check if we want to keep searching or we are lazy ones who love to depend on others.