freeCodeCamp.org Forum Curriculum Profile Basic JavaScript: Counting Cards Challenge

I am stuck in this challenge:

In the casino game Blackjack, a player can gain an advantage over the house by keeping track of the relative number of high and low cards remaining in the deck. This is called Card Counting.

Having more high cards remaining in the deck favors the player. Each card is assigned a value according to the table below. When the count is positive, the player should bet high. When the count is zero or negative, the player should bet low.

Count Change Cards
+1 2, 3, 4, 5, 6
0 7, 8, 9
-1 10, ‘J’, ‘Q’, ‘K’, ‘A’

You will write a card counting function. It will receive a `card` parameter, which can be a number or a string, and increment or decrement the global `count` variable according to the card’s value (see table). 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 current count and the player’s decision ( `Bet` or `Hold` ) should be separated by a single space.

Example Output
`-3 Hold`
`5 Bet`

Hint
Do NOT reset `count` to 0 when value is 7, 8, or 9.
Do NOT return an array.
Do NOT include quotes (single or double) in the output.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

And here is my solution:

``````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 = 1;

break;

case 7:

case 8:

case 9:

count = 0;

break;

case 10:

case 'J':

case 'Q':

case 'K':

case 'A':

count = -1;

break;

}

var count = count + card;

if (count > 0) {

return console.log(count + ' Bet');

} else {

return console.log(count + ' Hold');

}

// Only change code above this line

}

cc(2);
``````

Welcome, beka_lee.

For future posts, if you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Your count will always be either `-1`, `0`, or `1` because you always assign those values to `count` in your `select` statement. You need to be incrementing, decrementing, or doing nothing to count instead.

Then you have the following problem:

``````var count = count + card;
``````

Why are you adding the value of card (which could be “J”) to count?

Thank you very much! It is my first ever time coding JS and using freecodecamp, so I’m sorry for creating such mess in the forum.

Because I thought I needed to update the global scope value of count, i.e. save the previous value every time I run a new function with a different value.

For instance, initially I run a function like cc(2) - and my switch function would return the value +1. Then, I wanted this value to be stored in count.
Next, I run a new function cc(3) for example. cc(3) - and my switch function would return the value +1. And I want this value of cc(3) to be added to the previous value of cc(2) which is +1 + 1 = +2.

That is why I wrote

var card = card + count.

Am I right here?
I am open to any feedbacks

`count` is a global variable, so when you write:

``````count = -1
``````

you are changing the global variable to be `-1`. The problem is, the next time the function is ran and let’s say a card like `K` is passed to the function, you want to decrement `count` by 1. Your current function will just assign `-1` to `count` which erases the previous value of `count`. If the first 3 calls to the function pass `K`, you want `count` to be `-3`, but your function will still only make it `-1`.

To be clear, `count` depends on the value of `card`, but only changes by subtracting `1` from it or adding `1` to it. The value of `card` determines which of these actions is taken on `count`. The value of `card` should not be added or subtracted to `count`, because that would not make any sense.