Counting Cards //why is this not working?

Counting Cards //why is this not working?
0

#1

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


#2

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


#3

I fixed the 2nd count to

count==="-1"

Logically I think it shall work… what you think?


#4

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


#5

count ==="=1"

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


#6

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


#7
  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!


#8

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?


#9

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


#10

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.


#11

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?


#12

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.


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


#14

Look carefully at the instructions. They read as:

The current count and the player’s decision (Bet or Hold) should be separated by a single space.

Your code is returning a string like:

"3Bet"

instead of:

"3 Bet"


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


#16

The if statement above is basing what gets returned on the current card value instead of the current count value. Plus, it is only returning the count without the Hold or Bet concatenated to it.

As soon as a return statement is executed, the function immediately exits and stops processing any later code in the function.


#17

so this problem must be solved by the switch statement?


#18

No, you can use if statements, butlike you did with the Switch statement version, you must separate the increase/decrease logic of count from the logic which determines the actual string returned based on count. It is a two step process:

  1. Increment or decrement the count variable based on the card value passed into the function.

  2. Decide whether to return the count concatenated with " Bet" if count is greater than zero OR return count concatenated with " Hold" if count is not greater than zero.

That is all there is to this challenge.


#19

is this case there would if statement for each card?


#20

I will give you a hint for the cards 2 through 6 and cards ‘A’, ‘J’, ‘K’, ‘Q’

Think about how you could use one or more of the following comparison operators:

<

<=

>

>=