Counting Cards (Using Conditional Statements)

Tell us what’s happening:
I am having trouble understanding what I am doing wrong here. Can someone help me better understand the logic behind my future corrections, once they’re analyzed?

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  if (card > 2 || card < 7) {
    count++;
    if (count > 0) {
      return (count + " Bet");
    }
  }

  else if (card > 6||card < 10); {
    count;
    if (count >= 0) {
       return (count + " Hold");
    }
  }
  
  return "Change Me";
  // 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_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

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

Think about what the || operator is doing here.

Let’s say you are 30 years old. If I asked you to raise your hand if your age is greater than 10 or less than 20, would you raise your hand?

On a separate note, how do you plan to deal with cards like “A”, “K”, “Q”, “J”, and “10”?

Also, you need to separate the count calculations from what gets returned. Currently, you are basing your return on a combination of the card value and the count. The return value is ONLY to be based on the count value.

This was perfect advice, thank you! Just solved it with the solution below.


var count = 0;

function cc(card) {
  // Only change code below this line
  if (card > 1 && card < 7) {
    count++;
  }

  else if (card > 6 && card < 10) {
    count;
  }
    else if (card ===10||card==="J"||card==="Q"||card==="K"||card==="A") {
      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');

Good job!

One other question: Do you need this else if statement?

I don’t! Left in there from a previous version. Removed :+1:.

You can also make an array out of the cards, then check whether the card is in the array. I found that way much simpler. Stuff like:

else if (card ===10||card==="J"||card==="Q"||card==="K"||card==="A")

…can be written as:

else if ([10, 'J', 'Q', 'K', 'A'].indexOf(card) >= 0)

@swcarrey Please don’t put full working solutions. Just stick to the hints like you have done above.

Ok I understand, no worries.