Counting Cards Problems

Tell us what’s happening:
I don’t really understand what I am doing here. Perhaps because I am not very good with math. But I can’t seem to get a single green check mark here. What am I doing wrong?

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 7:
    case 8:
    case 9:
    count += 0;
    break;
    case 10;
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
    count -= 1;
    break;
    if count =< 0{
      return count + "Bet";
    } else {
      return count = "Hold";
    }
  }
  
  return count("Bet", "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; rv:60.0) Gecko/20100101 Firefox/60.0.

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

You have made a number of syntax mistakes.

  • Line 25: Condition is not enclosed in ( or )
  • Line 25: Comparison operator incorrect. Less than or equal to is written as <= not =<.
  • Line 18: Expecting : after case, instead found ;
  • Line 32: You are trying to recursively call and return the value of count() as a function when it is not a function.

OK, this

case 10;

should be a colon.

This

if count =< 0{

The condition needs to be in parentheses. And the logic is wrong and that operator doesn’t exist.

This whole block:

    if count =< 0{
      return count + "Bet";
    } else {
      return count = "Hold";
    }

(when corrected) needs to be outside of the switch. You want that every time.

This:

  return count("Bet", "Hold");

I don’t even know what that does.

Learn to use the browser console, usually something like ctrl-shft-j. There is a ton of useful information in there, not the least of which are the error messages for most of these problems.

You declared a variable named count and assigned it to an integer

but here you’re calling a function

Hi All,
I have managed to solve the above with the following code :

var count = 0;

function cc(card) {
  // Only change code below this line
  if (card === 2 || card === 3 || card === 4 || card === 5 || card === 6) {
    count++;
    return count + ' Bet';
  } else if (card === 7 || card === 8 || card === 9) {
     switch(count){
          case 1:
           return count + ' Bet';
    }
    return count + ' Hold';
  } else if (card === 10 || card === 'J' || card === 'Q' || card === 'K' || card === 'A') {
    count--;
    switch(count){
          case 1:
           return count + ' Bet';
    }
    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');

I am not sure if it is the best solution tho…
If someone can optimise it a bit will be perfect!!!

I think I got the syntax errors resolved but now the logic seems off.


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 ++;
    break;
    case 7:
    case 8:
    case 9:
    count += 0;
    break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
    count-;
    break; }
    if (count <= 0){
      return count + " Hold";
    } else {
      return count + " Bet";
    }
  }
  return count;
  // 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; rv:60.0) Gecko/20100101 Firefox/60.0.

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

There are a couple of problems here. First of all, remember to indent properly. If you had, you might have noticed the extra close brace. Taking a few seconds to indent and format properly will more than pay off in the long run. Just make it a habit. Never code any other way.

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 ++;
      break;
    case 7:
    case 8:
    case 9:
      count += 0;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count-;
      break;
  }
  if (count <= 0){
    return count + " Hold";
  } else {
    return count + " Bet";
  }
  } // <-- extra close brace?
  return count;
  // 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');

Once you take care of that extra curly brace, you have a typo on this line:

      count-;

Do you see it?

When I fix those two mistakes, your solution passes.

OMG man thank you. And thanks to all who replied to this, I took a little from each of you. I seriously drained some hours on this one. Learned some good lessons. Realizing adding an argument creates a function is not the least of them. Thanks all.

You need to use switch statements, its also very simple compared to that @nauagio13

2 Likes

@John-freeCodeCamp I will changed did and I will re-post it initially I did it with switch but I had stuck on the internal checks…:+1: