Counting cards lesson

Hi guys,
at this lesson

This is my code

let count = 0;

function cc(card) {
  // Only change code below this line
let Bet = count;
let Hold = count;

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

if (count > 0) {
  return count + Bet;
}

 break;
 case 7:
 case 8:
 case 9:
 count = 0;
if (count==0) {
return count + Hold;
}
 break;
 case 10:
 case 'J':
 case 'Q':
 case 'K':
 case 'A':
 count--;
if (count < 0) {
  return count + Hold;
}
}

  return "Change Me";
  // Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');

Can anyone point me directions where am I wrong, without giving me the solution, I checked the hints but no success lol. To me it looks like everything is correct, at least the switch etc but I guess I’m wrong somewhere lol.
I was having issues with return statement, so I declared variables, maybe the issue is there or maybe the switch / ifs? thanks in advance!

Edit: by checking my code again my variables are definitely wrong, but how am I supposed to add count + Bet without making it a string. if I don’t define Bet / Hold it says they are not defined.

The first test says:

Cards Sequence 2, 3, 4, 5, 6 should return 5 Bet

If I test your code with:

console.log(cc(2)); 
console.log(cc(3)); 
console.log(cc(4)); 
console.log(cc(5)); 
console.log(cc(6));

to see what it is returning, I see:

1
3
5
7
9

The last call is what concerns us. It is supposed to return “5 Bet” but it is returning “9”.

See if you can give it as shot.

One thing that did catch my eye:

 count = 0;
if (count==0) {
  return count + Hold;
}

Why do you set it to 0 and then immediately check if it is 0? And I don’t think we should be setting it to 0, but just “adding 0”. I think you are misunderstanding counting cards. The only reason you would set it to 0 is if a new deck was used, but that isn’t part of this challenge.

Also, I don’t think your returns should be inside the switch. I think the switch just checks how to affect the running count, and then after that you have logic for what will be returned.

And what is this?

return "Change Me";

but how am I supposed to add count + Bet without making it a string.

Why can’t it be a string?

The instructions say:

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative.

the return “change me” is the lesson’s code.

Why can’t it be a string?
the reason the lesson wrote in the hint “Do NOT include quotes (single or double) in the output”.
that’s why it confused me isn’t return is the output.

I don’t completely understand card counting like I think i sort of get it, so I try to create the solution. The idea I think if you count cards you know when to bet because you can make an assessment which cards should come next high or low, the assignment says to bet when enough low cards are dealt, so I guess that’s that.

I’ll try to work on it, i’ll update what comes up :slight_smile:

edit: looking at their code return change me I think they want me to change it and gave me a return hint over there, so i’ll try to work around it.

Right, and it was asking you to change it. :wink:

“Do NOT include quotes (single or double) in the output”.
that’s why it confused me isn’t return is the output.

No, I think “output” refers to the final string, that would get outputted to the string. I agree it is confusing wording, but I think they mean that there should not be quotes inside the string. You may end up using quotes to define the string literal.

The idea I think if you count cards you know when to bet because you can make an assessment which cards should come next high or low, the assignment says to bet when enough low cards are dealt, so I guess that’s that.

Yeah, that’s basically it. A 10 - A is a “good card”, and 2-6 are “bad cards”. You are keeping a running count of what you’ve seen so you can keep an idea of whether there are more good or bad cards left in the deck. A positive count means that there are more good card than bad cards left. If you’re still not clear, check out youtube videos - there should be plenty - this is a common trick for blackjack players.

edit: looking at their code return change me I think they want me to change it and gave me a return hint over there, so i’ll try to work around it.

Yup.

OK, give it a wack and check back if you get stuck again.

let count = 0;

function cc(card) {
  // Only change code below this line
let Bet = "Bet";
let Hold = "Hold";

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

 
 case 7:
 case 8:
 case 9:
 count = +0;

 
 case 10:
 case 'J':
 case 'Q':
 case 'K':
 case 'A':
 count--;

}
return count;
 
  // Only change code above this line
}

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

console.log(cc(2)); 

okay here is my code so far. I think there is something wrong with my count code, and I am not sure what if i make a check
console.log(cc(2));
the count gives me -1
but why?
i have case 2, and an increment at the end…

OK, I think we’re getting closer.

Just try one:

console.log(cc(2)); 

(Remember to remove the other calls to the function - we are using a global variable for the count so it is cumulative.

After this one call, I would expect the count to be 1 and the string to be "1 Bet". Instead, I get -1.

So there are two problems. Actually three…

First of all, why is it “-1”? In your switch you haven’t included break statements. This means that you will get “fallthrough”. Matches “case 2:” and then just jumps in there. So it increments count. But it didn’t have a break statement so the switch just keeps going, evaluating count = +0; and count--;. You need some break statements in there. Do some reading about switch statements.

The next problem is here:

count = +0;

What do you think that is doing? I think it is setting that variable to +0. Is that what we want? I would think that we would want the count unaffected. Did you mean += 0? That would have had the right effect, but I think it would be better to just let switch's default handle this and do nothing in these cases.

The last problem, you are supposed to return a string, a number followed by a space followed by the betting advice, either “Bet” or “Hold”. But this is your return statement:

return count;

You’re only returning the count.


When I fix those things, your code passes for me.

return count;
reason I only wrote it just to check my count because i saw that was a different issue with the fully written code.

the reason I haven’t added any breaks because I planned to add ifs inside my switch with the necessary returns, google said it okay to do it :slight_smile: is it the right practice tho?

The logic for the return statements doesn’t depend on the card. It is only dependent on the count. Yes, you could put that logic in three places, inside your switch statement, but it makes more sense to do it in one place instead of 3.

I mean, yes, if you had return statements inside all of your cases, you would not need breaks. That is true and is useful in some situations, but not in this particular one.

Does that make more sense?

Yes it does.
How can I know for the future which is a better practice either use 1 logic, or seperate logics?

another question

count = +0;

if I add that one into the switch default don’t I still have to type count +=0?

Experience. The more things like this you solve, the more your “Spidey Sense” will get developed.

another question

count = +0;

if I add that one into the switch default don’t I still have to type count +=0?

Well, there is a big difference between count = +0 and count +=0. In the first one you are setting it equal to (positive) zero. You are effectively resetting the count. As I explained, we don’t want to do that for this function. Really for these cases (7, 8, 9), we don’t want to do anything. And that is what you can have in your default - doing nothing. Yeah, I guess you could have count +=0 since that basically does nothing, but it is a wasted line.

1 Like

I have completed the challenge, I was stuck again and wasn’t sure why after I added the breaks; my count became undefined after an hour of break I saw one of the } was in the wrong spot… lol

1 Like

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