Counting Cards question

Counting Cards question
0

#1

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??

Your code so far


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
}

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

Your browser information:

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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/


#2

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.
Does this help you understand how this works?


#3

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?


#4

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


#5

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.


#6

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


#7

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


#8

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


#9

THANK YOU THANK YOU THANK YOU

you are the best, thanks for helping :slight_smile: