Counting Cards question

Tell us what’s happening:
Hey guys I’m a little clueless on how the correct code should look for this challenge. I don’t understand how the function cc(card) method can take a sequence of card inputs like:

cc(3); cc(4); cc(‘A’); cc(2); cc(7);

and return a calculated answer from these multiple inputs, the way I’m seeing it is cc(3); is a separate call with its own separate return value. As well as cc(4); and so on. So the way I’m seeing it is the sequence above returns 5 separate values. even though the global variable count gets increased/decreased with every call. So cc(3); returns ‘1 Bet’ and count now equals 1, next cc(4); adds 1 to count making it 2 and the return value is ‘2 Bet’ . So how does this take all 5 of those inputs, add their values to the count respectively and then return the final value??

``````
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 7:
case 8:
case 9:
count = count;
break;
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
}

if(count > 0) return count + 'Bet';
else if(count < 0) return count + 'Hold';

// Only change code above this line
}

// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36`.

yup true that each call to cc is a separate call with a different input parameter.
The thing you missed in the code is the global variable declared at the top called ‘count’.
If you are familiar with global variables, then you will know their scope is the whole program and so they keep their value until the whole program execution is done.

Yah I knew that, but the code I wrote makes sense to me yet I failed every test, you mind taking a look at it?

taking a very quick glance, I immediately see you are missing some spaces in your Bet and Hold return strings.
Remember the answer has to have a number, then a space, then either the word Bet or Hold. (space is needed)
Also you may want to handle what happens when count == 0

Just added the spaces and instead of count == count; i did count += 0; I passed a few more tests but I’m still failing most of them. Here are the ones I failed:

Cards Sequence 7, 8, 9 should return 0 Hold
Cards Sequence 10, J, Q, K, A should return -5 Hold
Cards Sequence 2, 2, 10 should return 1 Bet
Cards Sequence 3, 2, A, 10, K should return -1 Hold

Also thank you for helping me out, I really appreciate it.

I passed these ones:

Cards Sequence 3, 7, Q, 8, A should return -1 Hold
Cards Sequence 2, J, 9, 2, 7 should return 1 Bet
Cards Sequence 2, 3, 4, 5, 6 should return 5 Bet

You need to add some handling for count==0 as I mentioned. Specifically here in the section that figures out what to do…

also you are missing the handling in the switch for ‘10’

THANK YOU THANK YOU THANK YOU

you are the best, thanks for helping

1 Like

i didn’t understand why we should put the var count in global
when i put it in the function it didn’t increment or decrement still give me 1,1,1, ect why even , when we put the var count in the function it should be global for the switch and the if functions because they are both in the function cc(card) ?

The count variable must be global because the tests use it to test the function.