Counting Cards - Not Accepting Solution

I’m stuck on this challenge as the application will not accept my answer. However, when I run the code below in Visual Studio Code, the console shows that everything outputs as per intent of the code. It seems to output as it should in my browser (Chrome, latest), as well, but I cannot get a “passed” score to come up.

I have have even gone so far as to copy the code directly out the solution in the video, dropped it in the browser window, and that doesn’t work.

Thanks in advance for any direction you can provide!!

Here is the code and console results:

var count = 0;

function cc(card) {
  // Only change code below this line

  var count = 0;
  switch(card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count++;
      break;

    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count--;
      break;
  }

  var holdbet = 'Hold'
  if (count > 0) {
    holdbet = 'Bet'
  }

  return count + " " + holdbet;
  // Only change code above this line
}

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

CONSOLE OUTPUT:

1  Bet
1  Bet
1  Bet
1  Bet
1  Bet
0  Hold
0  Hold
0  Hold
-1  Hold
-1  Hold
-1  Hold
-1  Hold
-1  Hold

Hello and welcome to the freeCodeCamp community~!

You are re-declaring your count variable inside your function, which prevents the function from properly tracking the cards that are dealt.


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 (’).

1 Like

nhcarrigan,

Thanks so much for the quick reply and the direction. I removed the repetition from the code block and I was able to pass the exercise. However, removing the 2nd declaration of the variable changes the console output of the Case statements “Bet”, where as I would have expected the result to turn out as it did in my original post.

What am I missing?

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

By removing that second declaration, you’ve returned count to the original declaration which is a global variable.

This means that every function call affects the value of count, and it does not get reset (this is important for the challenge). So each time you call the function, count gets incremented or decremented based on the value of the card.

Thanks for providing me a V-8 moment . . . I get it now!! Thanks much!!!

And the second declaration inside the function code block resets the count to ‘0’ with each function call.