Help Please! Card Counting Challenge - Javascript

I’m confused about how to write the function to get the output it’s asking for. I don’t know how to write it so that it adds or subtracts from the global count and returns a “number and string”. Please help. Thanks!

Hi there,
In the spirit of the FCC forum, I think what you are asking for is guidance, not an answer. And the only way to receive guidance is to give us more information on what you’ve tried so far. Maybe an example of your code or some idea of what you tried so far.

1 Like

Hi! Thanks!
This is one of the things I’ve tried so far:

if (2 || 3 || 4 || 5 || 6) {
      return = count +1 "Bet";
  }

I don’t know if I’m supposed to use if/else, or switch. Also, I don’t understand how to make it so the answer comes out adding or subtracting to the global number AND give “Bet” or “Hold” in the same line. Did I miss something on a prior lesson? I’ve been going through the past lessons to try to figure it out.

My first line was:
if (card >= 2 && card <=6) {
but I think your version works too.
You’re using return too soon though.
Think about laying down all possible card values first before returning anything, because there are two possible outcomes, once you’ve gone through the deck. So you’re correct in adding 1 but you’re returning something before evaluating cards 7 to Ace. Only once you’ve evaluated all possible cards can you possibly return anything.

To get the line
return = count +1 “Bet”;

working you need to concatenate the two strings (ie the count and the word bet). Something like

return = (count +1) + " Bet";

As @Soupedenuit mentioned you are missing some other elements though. You probably want to be updating the global count value at this point rather than returning it, then evaluating the count and returning the appropriate result at the end of your function.

Thanks. So, how does one update the global value? I think that’s what I’m stuck on.

Thank you. So, is it supposed to be a long formula before I can return anything?

Yes, in my case it was achieved with an “if” statement followed by several “else if” statements, then by two possible return outcomes.

Ok. So I came up with this:

var count = 0;

function cc(card) {
  // Only change code below this line
  if (card >= 2 && card <= 6) {
    count++;
  }
  if (card == 7 || 8 || 9 ) {
    
  
  }
  if (card == 10 || 'J' || 'Q' || 'K' || 'A') {
    
   count--;
  }
 
if (count > 0) {
   answer = (count + "Bet");
}  
  if (count <= 0) {
  answer = (count + "Hold");
  }
  
  return answer;

  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

But it’s still wrong.

You’re getting close. You need a space between count and ++.
Also, starting from the second statement it should be else if until you’re done evaluating all the cards. Also, add a space in front of your Bet and Hold strings like this: " Bet" and " Hold".

…also, since 7, 8 & 9 don’t change anything, you can omit the statement altogether. The simpler the better.

1 Like

Thanks!
Here’s what I got:


function cc(card) {
  // Only change code below this line
  if (card >= 2 && card <= 6) {
    count ++;
  }
  
  else if (card == 10 || 'J' || 'Q' || 'K' || 'A') {
    
   count --;
  }
 
if (count > 0) {
   answer = (count + " Bet");
}  
  if (count <= 0) {
  answer = (count + " Hold");
  }
  
  return answer;

  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

It’s still missing something. =(

else if (count <= 0)

Try that. If it doesn’t work, maybe it’s because you didn’t declare “answer” - not sure.

Even just using
if (count > 0) {
answer = (count + " Bet");
} else {
answer = (count + " Hold");
}

Should be fine as if count is not greater than 0 it must be less then or equal to 0 so there is no need to check that condition again.

declaring answer at the start of your function should get it working.

Thanks. Still doesn’t work.

var answer;
var count = 0;

function cc(card) {
  // Only change code below this line
    if (card >= 2 && card <= 6) {
    count ++;
  }
  
  else if (card == 10 || 'J' || 'Q' || 'K' || 'A') {   
   count --;
  }
  
  else if (card === 7 || 8 || 9) {
    count += 0;
  }
  
if (count > 0) {
   answer = (count + " Bet");
}  
  if (count <= 0) {
  answer = (count + " Hold");
  }
  
  return answer;

  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(7); cc(8); cc(9); cc(); cc();

There seems to be a problem with the (7, 8, 9) card count. I added whats there now. But even without it, it doesn’t work. It isn’t calculating those cards correctly.

The issue stopping your program from working is the line:
else if (card == 10 || ‘J’ || ‘Q’ || ‘K’ || ‘A’) {
This is the equivalent of asking if:
card is 10 or ‘J’ or ‘Q’ etc.
rather than
card is 10 or card is ‘J’ or card is ‘Q’ etc.
Javascript sees values like ‘J’ as true and as such will always execute this if statement in it’s current format. See here for some details on how to change this.

Additionally I would suggest looking at the following:
else if (card === 7 || 8 || 9) {
count += 0;
}

The above section is not needed, your checking a condition then adding 0 to count (ie not changing it) if the condition is true. Removing this section results in the same action (ie nothing is added to count) without the need for any code.

Answer is not a global (or at least it doesn’t need to be), so it should be declared within your function.

Thank you! I got it. I don’t totally understand why it wouldn’t work the way I had it down. But thank you! I have something to study a bit more. =)

1 Like

Hi everyone! It took me quite a while to figure this one out but I finally got it. Thanks to reading and rereading this thread i was able to make sense of it all . Here’s my code that passed the test after the 30th attempt. I’m so excited to share. Hope this helps anyone. Thank you again to all who posted questions and responses on this thread. You guys are the best!

var count = 0;

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

if (card >= 2 && card <= 6) {
count = count+1;

} else if ( card >= 7 && card <= 9) {
  count = count+0;

}
else if (card == 10 || ‘J’ || ‘Q’ || ‘K’ || ‘A’) {

count = count-1;
}

if (count > 0) {
answer = (count) + " Bet";

} else if (count === 0) {
answer = (count) + " Hold";
}
else if (count <= 0) {
answer = (count) +’ Hold’;
}

return answer;

In addition, in order to make sure there is a space between the count and" Hold/Bet", you have to have a space following the opening quotation mark on the string. This was one of the things that kept me from passing the challenge.
Ex: Correct way - (count) + " hold"; notice the space after the opening quotation
Wrong way - (count) + “hold”;

1 Like

Hi
I too spent some time on this but came up with a differant solution which past the test. I dont want to give away my solution but would say only that I did not use || but a switch in the 2nd condition.
It worked for me where the || wasnt. Thanks to all above - you helped me reach my solution.

1 Like

Hello everyone.

Same with @Joods, and as @Soupedenuit said with FCC spirit, I won’t share my code to make you guys keep digging your own solution. I only want to recall this lesson for you to use switch. It’s a bit long code but clear enough to get rid multiple identical options for the card value. Rather than using if else with comparison that may a bit intimidating for a visualy person like me :smile:

Besides, as its advantage, it’s better to use switch for selecting many options so your code would be more readable.