Counting Cards - Basic Javascript Module

Hi all,

I compared my code with the answer provided in the discussion (link below) but I cannot see the difference between the two. I have provided my code below and it does not work, whereas if I copy-paste the answer, it works. Can someone please help me find the differences? Thank you.

The question for the Counting cards exercise is:
In the casino game Blackjack, a player can determine whether they have an advantage on the next hand 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 Outputs: -3 Hold or 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.

My solution was

let count = 0;

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

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;

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


  return "Change Me";
  // Only change code above this line
}

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

The correct solution is below as shared in this post: freeCodeCamp Challenge Guide: Counting Cards

let count = 0;

function cc(card) {
  // Only change code below this line
  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;
  }
  if (count > 0) {
    return count + " Bet";
  } else {
    return count + " Hold";
  }
  // Only change code above this line
}

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

Any help would be great!

Here are some formatting fixes for your code:

let count = 0;

function cc(card) {
  // Only change code below this line
  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;
  }
  if (count > 0) {
    return count + "Bet";
  } else {
    return count + "Hold";
  }

  return "Change Me";
  // Only change code above this line
}

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

Try adding console.log(cc(10)); at the very end of the code here. You should see that you have a small spacing problem that means your output doesn’t match the requested perfectly.

1 Like

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