Counting Cards (error on cards 7, 8, 9)

Tell us what’s happening:
Hey tribe :slight_smile:
I’m only getting error messages coming up for cards 7, 8 and 9.
Would someone please help me connect the dots?

Truly grateful

Your code so far

var count = 0;

function cc(card) {
  // Only change code below this line
  if (card <= 6) {
    count ++ ;
  } else if (card == 10 || 'J' || 'Q' || 'K' || 'A') {
    count -- ;
  if (count <= 0) {
   return count + " Hold";
  } return count + " Bet";

  // 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 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:

Sup tribe memeber @pinealfriend.

The first thing I noticed is that you are missing a if condition for the 0 hold. That seems like a actual string you are supposed to return such as if....return "0 hold". The rest? is for you to discover :heart:

Ah another tip, might be better to fully write each condition out, that way you 100% certain that one condition might not be playing with another, also keep a sharp eye on the flow of the logic.


This is not correct

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

You need to check the card against the letters like you are with the number

else if (card == 10 || card == 'J' ...etc)

Thanks for your responsive advice @Hazecode

If my if statement (as above) includes ’ <= ’ symbol, should that not include the conditions for the 0 hold? Because it includes equal to 0?

Or do you mean that I need to specify adding zero for cards 7, 8 & 9? I am just not sure why I need to add a condition for these cards if the count is not changing as a result.

Thanks for helping me clarify

Thanks @lasjorg !

What surprised me is that I did not get any error messages for those cards (J, Q, K & A). Only received error messages for 7, 8 & 9. Could you please help me understand why that is? Why is it reading my code which is incorrect (as you generously pointed out above) and not returning an error on those cards?

edit: really appreciate your advice in helping gain a deeper understanding of what is going on!

lol! I thought that was another js feature! I thought “wow, you can chain ifs like that?” I guess not but js does not surprise me really :rofl:

Sorry, I didn’t get back to you, it was really late for me. I’m not sure I understand the question, you should have been failing 3 tests, not 1, was that not the case?

Anyway, what happens with the if statement you had is type coercion. The strings are coerced to true, because of how JavaScript works they are considered a truthy value.

So you basically had this:

if(test === 10 || true) {

Another example:

const test = 1;

if(test === 10 || 'somestring') {
  console.log('true'); // logs out true
} else {

This is also sort of related to the “trick” used to give default values. Because we have default function parameters now, it is a little less used.


function printName (name) {
  // If name is undefined it is a falsey value, so the string is used
  name = name || 'Please enter your name';

printName('John Doe') // logs John Doe
printName() // logs Please enter your name