Counting Cards //why is this not working?

Tell us what’s happening:

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==="+1";
    break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
    count==="-1";
    break;
  }
  if (count==="+1"){
    return "Bet";
  }else{
    return "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 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15.

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

Your code says:
if the card is 2 or 3 or 4 or 5 or 6 then
is count equal to “+1” and is it a string?
done.

Your code also says:
if the card is 10 or ‘J’ or ‘Q’ or ‘K’ or ‘A’ then
set count to be the string “-1”

Does the above explanation help you understand why the code doesn’t work?
(is the code behaving the way you wanted it based on my explanation ?)

1 Like

I fixed the 2nd count to

count==="-1"

Logically I think it shall work… what you think?

I noticed in the guide the return is count ++ and –
I don’t understand why this shall work and not the other?

count ==="=1"

Sorry correction:
This line compares the count variable with the literal string “=1”
Is that what you want to do? compare?

1 Like

ok. so no string?
but what if it’s a string… if the count is equal to this string return bet or hold!!

  switch (card)
  {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    count===+1;
    break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
    count===-1;
    break;
  }
  if (count===+1){
    return count + "Bet";
  }else{
    return count + "Hold";
  }
  

removed the string but still not working!

now this says:

if card is 2/3/4/5/6 then
compare count to +1

Again, is that what you are trying to do? To compare something?

1 Like

no, I’m trying to return the value of the count as +1

ok that doesn’t really match the description of the problem though. So let me explain what +1 or -1 means in the challenge.
What they want is for you to count up or down depending on the card you see.

If you want to count up for example, you add 1.
If you want to count down , you subtract 1.

I’m assuming you know enough javascript to be able to code that…
If not, I can find some of the old challenges for you to help you learn that.

1 Like

Actually I went though previous challenges trying yo find out what am I missing. But I got stuck.
so to add value of the count by one I return count++ and to count-- to subtract?

yup you got it! There are other mistakes in your code though (but I didn’t point them all out)

Be careful and read the instructions to compare what you are doing with what they want.

1 Like
  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";
  }
  

would you plz point at the mistakes here?

if (card===2||card===3||card===4||card===5||card===6){
  return count++;

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

why is this not right?

so this problem must be solved by the switch statement?

is this case there would if statement for each card?

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

how to make the code not stop here and go to a 2nd if statement for the count value and bet or hold?

and can these comparison operators work with strings “k” “A”?

  if (card>=2 && card<=6)
  {
     count++
  }
  
  
  else if (card>=7 && card<==10||card==="J"||card==="Q"||card==="K"||card==="A")
  { 
     count--
  }

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

is this close?

0 and we shouldn’t count it.

Exactly, so don’t include those numbers in your else if logic. With your last code posted, you would be subtracting one from count for number 7, 8, or 9.