# 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

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.

â€ś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 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 `break`s. 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.