Counting Cards - my code should be fine....or not!

Hi everyone, I’m currently trying my own solution to the test mentioned in the title.
I know my code might look terrible to you and that there are faster options in the forum, but I just wanted to know why it doesn’t pass the test as it is, it gives the same exact result as the official solutions, so what am I missing? I really want to learn something here.

let count = 0;

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

  if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6) {
  (count += 1);
    if (count > 0) {
      (count + " Bet"); 
      return;}
    if (count == 0 || count < 0) {
     (count + " Hold"); 
      return;}
  }
  else if (card == 7 || card == 8 || card == 9) {
  (count += 0);
    if (count > 0) {
      (count + " Bet"); 
      return;}
    if (count == 0 || count < 0) {
     (count + " Hold"); 
      return;}
  }
  else if (card == 10 || card == "J" || card == "Q" || card == "K" || card == "A") {
  (count -= 1)
      if (count > 0) {
      (count + " Bet"); 
      return;}
    if (count == 0 || count < 0) {
      (count + " Hold"); 
      return;}
  }
  else {
  return "Change Me";
  }
  // Only change code above this line
}

if the code works as is, but fails the test, then look at the challenge requirements, I’m pretty sure there is something specific that they want you to use.

I thought the same, but if I read the specifications:

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

It does exactly that, it isn’t mentioned anything else regarding the ways I should use to get the output, what’s more interesting is that I could use a function and the test will pass, but functions are shown later in the course.

Within few seconds I saw conflicting things and I know why it fails.

This is what is required from you.

But what does this do ?

Yes, it resets the count to 0, you’re supposed to do nothing if one of this is the given card value

Edit: Fast thinking doesn’t gives good results does it? , anyway you’re just adding 0 to the count value, which is again, not needed at all.

I thougth it just didn’t add anything, in fact if I run the code with these values:
cc(3); cc(2); cc(2); cc(7); cc(2);

my output is:

1 Bet
2 Bet
3 Bet
3 Bet
4 Bet

It doesn’t look like it resets the count to 0, still I’ve modified it but it doesn’t pass the test yet…it must be something different

Once I went to copy your code in the editor for the challenge I noticed something.

Why are you using this quotation marks?

whatever is inside them is not string literal.
also they throw an error.
SyntaxError: unknown: Unexpected character ‘“’.

Ah, it’s a mistake of the copy and paste, I get that error too if I copy and paste from here to the editor, you have to change them with single q.m. manually, I have no idea how it happened.
So after you fix that you’ll be in the same situation as I am, the code gives the expected result but I can’t pass the test.

take a look at this comment, another mishap.

1 Like

Oh my god…I copied and pasted the code and forgot to put the return BEFORE the rest…I pass the test now…ahahah, I knew it was something that obvious that I couldn’t see it, thank you so much!

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 have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Ah thank you, that would’ve avoided the quotes misunderstanding, good to know!

A lot of people lately have been not using the Ask for Help button and instead making forum posts without including formatting or the challenge link. I’m not really sure why this is happening so much?

Can’t talk of others, but I personally have not seen a guide in any form for things related to the forum, There are those scattered replies everywhere but searching for them is not convenient.

So if there is such a post that explains all those things, it shall be pinned so that it’s always shown on the screen above the other posts.

Ah you’re right, I knew I was missing something, I clicked on “get a hint” and then, after being directed to the forum, I continued here. It’s probably just that, people click on get a hint and forget to go back.

1 Like

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