Basic JavaScript: Counting Cards Solution and Answer match but can't move to next challenge!

Just wondering if anyone can spot the difference between my code and the solution? Because it’s not letting me pass but I can’t currently see any difference. Any help much appreciated, tia!

Here’s my code:

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 ++;
  break;
case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count --;
}

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

  // Only change code above this line

}

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

And the solution:

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');
1 Like

You should wrap that full working solution in in detail tags and your code in three backticks

Anyway to answer you:
You are missing a break somewhere
Also instead of returning, there is somewhere where you are logging to the console.

1 Like

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

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

your issue is that in about 50% of the cases your function returns undefined

1 Like

Thanks, I’ve managed to find the solution now. Thanks for your help!

1 Like