Basic JavaScript - Counting Cards

I don’t know if this is a big deal or not but it’s bothering me. I’ve done research here and elsewhere but can’t seem to find a definitive answer.

I’ve passed the challenge but I can’t get the console to show the result of the function that matches the string in the tests section? Does anyone else have this issue?

console.log(cc(2), cc(3), cc(4), cc(5), cc(6));
console.log(cc(7), cc(8), cc(9));
console.log(cc(10), cc(‘J’), cc(‘Q’), cc(‘K’), cc(‘A’));
console.log(cc(3), cc(7), cc(‘Q’), cc(8), cc(‘A’));
console.log(cc(2), cc(‘J’), cc(9), cc(2), cc(7));
console.log(cc(2), cc(2), cc(10));
console.log(cc(3), cc(2), cc(‘A’), cc(10), cc(‘K’));

It seems like I’m not getting a cumulative increment/decrement result.

This shows the following in my console:

1 Bet 2 Bet 3 Bet 4 Bet 5 Bet         - instead of - "5 Bet"
5 Bet 5 Bet 5 Bet                     - instead of - "0 Hold"
4 Bet 3 Bet 2 Bet 1 Bet 0 Hold        - instead of - "-5 Hold"
1 Bet 1 Bet 0 Hold 0 Hold -1 Hold     - instead of - "-1 Hold"
0 Hold -1 Hold -1 Hold 0 Hold 0 Hold  - instead of - "1 Bet"
1 Bet 2 Bet 1 Bet                     - instead of - "1 Bet"
2 Bet 3 Bet 2 Bet 1 Bet 0 Hold        - instead of - "-1 Hold"
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14909.132.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

This does match. The last one in the series of calls returns 5 Bet.

The reason the last call of the other lines do not match is that the test resets count back to 0 before each test (series of calls to cc).

So if you were to write the following after your function, then you would see that the last call of each series matches what the tests are expecting.

console.log(cc(2), cc(3), cc(4), cc(5), cc(6));
count = 0;
console.log(cc(7), cc(8), cc(9));
count = 0;
console.log(cc(10), cc('J'), cc('Q'), cc('K'), cc('A'));
count = 0;
console.log(cc(3), cc(7), cc('Q'), cc(8), cc('A'));
count = 0;
console.log(cc(2), cc('J'), cc(9), cc(2), cc(7));
console.log(cc(2), cc(2), cc(10));
count = 0;
console.log(cc(3), cc(2), cc('A'), cc(10), cc('K'));


1 Bet 2 Bet 3 Bet 4 Bet 5 Bet
0 Hold 0 Hold 0 Hold
-1 Hold -2 Hold -3 Hold -4 Hold -5 Hold
1 Bet 1 Bet 0 Hold 0 Hold -1 Hold
1 Bet 0 Hold 0 Hold 1 Bet 1 Bet
2 Bet 3 Bet 2 Bet
1 Bet 2 Bet 1 Bet 0 Hold -1 Hold

Thanks for the reply. So, you mean the count is not resetting for each series? If so, I understand. In actuality I am getting a cumulative result for each card instead of each “hand”. Is there a way to write this so that you’re only getting one final result for each “hand”?

This kind of brings me to another question I’ve had throughout the function challenges. Where do return statements go? While I have been able to pass each challenge I can never see the function returns without using console log.

No, the count is resetting for each test (a series of calls to cc).

I will just address this part of your question.

Returns go nowhere if you don’t store them somewhere.
So you call a function and it returns say a number.
If you want to store it in a variable, you can, then use it later to do something else.
or you can call a function and have it return and pass that return to another function and have it return etc. eg:
console.log(add(multiply(subract(5,4), 6), 7));

@BohdanHoh Can you give an example of what you mean in terms of calling cc? Show how you would make the call and what the expected result would be.

I’m sorry , but now I’m confused again. Are you saying the test resets the count to zero for each function call series but the code itself is not?

Also, the challenge says "Do NOT reset count to 0 when value is 7, 8, or 9 but when we don’t my log shows “5 Bet”. This would be the wrong answer if I were really using the “program”. I feel dumb, what am I missing…

I guess I’m imagining this being used at a blackjack table. Wouldn’t you want the “program” to just tell you, based on what showing, “5 bet” or “0 Hold”, instead of a string for each card in the function call series?

Yes, that is what I am saying. When a test runs, it will set the global variable back to 0 before it makes the calls using your version of cc shown for the test. After the calls are complete, it checks what the global count variable is (using the calls to your function) compared to what it expects it should be.

That hint is just telling you that cards 7 through 9 do not affect count anyway, so do not try to adjust count with your code for them.

I suppose, but that is not what this challenge is wanting you to do. The point of these challenges is to learn how to understand the requirements of a program to be created regardless if it is the way you may prefer to play it.

I mean the function still returns the correct calls for any given card. It is just that the tests call the function multiple times in a given set to see if you have correctly coded it.

If I am counting cards playing Blackjack, I don’t try to keep track of the count as they are dealt to each person. Once all the cards come out for all players, I quickly scan across the table to see all the cards and make the calculations as I scan. Looking at each person’s card as they come out would draw unwanted attention at a Blackjack table.

If you think of each test as several cards being dealt to multiple players, then this is a real life application of the game.