Card Counting Basic JS

Below is my answer for this exercise. It is exactly the same as listed for “Solution 1”, line-by-line. However, it fails every test. I’m not sure what’s occurring.

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

I’ve checked it against Solution 1, and it’s identical from what I can tell. What am I missing here??

Thanks

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.

Please 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.

markdown_Forums

Try adding

console.log(cc());

at the end of your code. Does this help you see what’s going on?

1 Like

It helps. I notice that that count is maintaining it’s 0 value. How can the solution be exactly the same as the my code but still doesn’t work?

Hello~!

Actually, I show your count successfully changing. Take a look again at what each function call returns. :slight_smile:

1 Like

That sequence of calls just so happened to result in count being 0. You are successfully changing count. The issue is in the string that you return. Your output is not quite correct.

1 Like

The only change I made to the code now is the

console.log(cc(2), cc(3), cc(7), cc(“K”), cc(“A”));

and now it works.

I’m still super confused. I understand the solution but why did that change the outcome?

That change should not have caused the tests to start passing. You are still returning the wrong thing, unless you changed another part of your code. :slight_smile:

1 Like

This only changed what you are printing to the console. You still have the issue with your code.

1Bet 2Bet 2Bet 1Bet 0Hold

Doesn’t this look funny?

2 Likes

Wow, I feel very dumb now. I forgot the space. Thank you for the prompt answers and patience. Consider it solved!

3 Likes

These sort of little bugs happen to all of us : )