Counting Cards problem tell me what is wrong?

Tell us what’s happening:

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  if( card == 2 || 3 || 4 || 5){
    count++;
  }else if(card== 10|| 'J'|| 'Q'|| 'K'|| 'A'){
    count--;
  }else{
    count;
  }
  
  if(count> 0){
    return (count) + "Bet";
  }
  else{
    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 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

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

You should always use === when checking for equality as == does not check whether the type is also a match. I do not think this will affect the outcome of the test but it is best practice for the future.

  if( card == 2 || 3 || 4 || 5){
    count++;
  }else if(card== 10|| 'J'|| 'Q'|| 'K'|| 'A'){
    count--;
  }else{
    count;
  }

This block of code is not including the number “6” which needs to be accounted for and bunched with the first line of numbers.

I have also noticed the way you are chaining your ‘or’ statements is not correct. You need to reinitialise the variable. So, saying code === 10 || 'A' || 'B' is not the same as code === 10 || code === 'A' || code === 'B'

finally, your return statements do not seem to be correct. Try using the ES6 backticks. Here is an example of the first return statement using ES6:

  if(count> 0){
    return `${count} Bet`;

Thanks for the suggestion and your tips I really appreciate that and your correction had made a few answers right…thanks again

You could use a switch {} block

1 Like

That is ok mate. Everything I mentioned is what I changed from your code to make it pass all tests.

The main difference, for me, is that I simply used:

card < 7 && card > 1 

As the logic for the first if function. As mentioned by @leebut, you can use a switch statement also, I personally choose to use the if function just as it is less typing.

1 Like

Yes, in this case, a conditional is more succinct.

1 Like

not always… in 99% of the time yes …there is however a good case to use == which is when you need to check for null or undefined only.