Basic JavaScript - Counting Cards

Tell us what’s happening:

I already got the test, but I don’t really how it worked.

I hope I find someone who understands what I’m trying to point out.
Like, I think I know how the cards got calculated, like this:

if we have cards:
K, 8, 2, 3, Q, A

That means, we have:
-1, 0, +1, +1, -1, -1 = -3+2 = -1
==> -1 Hold

But I think, that calculation I did it’s not reflected in the code

OR

Is it that
count++ means adding up all the positive cards, say five of them (2,3,4,5,6) making +5
and
count-- means adding them up also (10, “J”, “Q”, “K”, “A”) making -5

And, by chance, we have mixed cards like 2,7, Q, 5,J how come we got the output 0 Hold

But. this following code doesn’t depicts the calculation:

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

I HOPE I FIND JUSTICE WITH THIS COMBAT I’M HAVING. PLEASE HELP ME. THANKS.

That’s my code below:

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

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Hi

The count variable is a global variable - it’s defined outside your function.
And the function is called repeatedly.

Try adding
console.log(count)
just before your if statement, to see what happens.

Hmm, okay I did, but didn’t get what output means. It’s well.

Anyways, Please can you tell me what this mean on the function:

        cc(4), cc("Q"), cc(7)

and all

Thanks.

If you mean

cc(4); cc("Q"); cc(7);

then that’s your function being called three times.

The first time, it’s called with card=4
cc(4);
The second time, it’s called with card=“Q”
cc("Q");
And the third time, it’s called with card=7
cc(7);

Okay, so that means

4, Q, 7
= +1-1+0
=0
==> 0 Hold

am I corect Sir?

That’s absolutely correct.

The(global) count variable starts at 0, and each successive call to your function then increments or decrements the count variable based on the card value.

Does that make more sense now?

1 Like

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