Basic JavaScript: Counting Cards - Got all but two tests to pass

On this challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards , I got all but two of the tests to pass. Here’s my code:

var count = 0;

function cc(card) {
  // Only change code below this line
  let retValue;
  switch (card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count += 1;
      retValue = count.toString() + " Bet";
      break;
    case 7:
    case 8:
    case 9:
      retValue = count.toString() + " Hold";
      break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count -= 1;
      retValue = count.toString() + " Hold";
      break;
  }
  
  return retValue;
  // Only change code above this line
}

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

The tests that aren’t passing are:

Cards Sequence 2, J, 9, 2, 7 should return `1 Bet`

and

Cards Sequence 2, 2, 10 should return `1 Bet`

What am I missing? I also tried if-else chain earlier but it made even less of the tests pass.

“Bet” or “Hold” in the return value is not based upon the value of the card but rather the value of count. You should create your return value after the switch.

I have this now:

var count = 0;

function cc(card) {
  // Only change code below this line
  let retValue;
  switch (card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count += 1;
      break;
    case 7:
    case 8:
    case 9:
      break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count -= 1;
      break;
  }
  
  retValue = count.toString() + " Hold";
  return retValue;
  // Only change code above this line
}

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

But this also makes the first test fail in addition to the two that were already failing before.

Well, now you are always adding Hold. That doesn’t match the instructions or my hint.

You should add Hold when count is 0 or less and Bet when count is greater than 0.

Before you were adding Hold or Bet based upon the value of the card, which will also not give you the correct result.

Okay, got it. Adding the check after the switch statement did it.

1 Like