Basic JavaScript: Counting Cards. Please Help!

Tell us what’s happening:

Your code so far


var count = 0;
function cc(card) {
  // Only change code below this line
  if(card >= 2 && card <= 6){
    count++;
    return count + 'Bet';
  }else if(card >= 7 && card <= 9){
    count += 0;
    return count + 'Hold';
  }else if(card = 10 || 'J' || 'Q' || 'K' || 'A'){
    count--;
    return count + 'Hold';
  }
  if(card > 0){
    return count + 'Bet';
  }else{
    return count + 'Hold';
  }
  
  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 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.125 Amigo/61.0.3163.125 MRCHROME SOC Safari/537.36.

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

count++ in the following line, do the same thing as count += 1. Thank you @snowmonkey for correcting the mistake.

The problem in the following lines here is that you are assigning number 10 to card. In JS: = is used for assignment and == or === are used for comparison. I suppose you already know the difference between == and ===. (If you have no idea, let me know.)

The second problem is that you are using the operator || but after that there’s no comparison, you should here try to compare card again with J, Q, K and A: …|| card == 'J' || card == 'Q'… etc.

Then the following code is going to be with no use, unless, you try to check whether count > 0, count == 0 and else you output the right string.

Of course, if you do this then, you should take off the return statements in the else ifs of the previous code.

I hope this was useful,
and it’s up to you now to figure out how to fix these and come up with a better solution. (Let us know if you have anything new, I will be happy to help you further.)

So a small correction: count +=1 and count++ return the same value. The latter is referred to as a ‘post-fix increment operator’.

1 Like

Thank you all. I solved the problem.

1 Like

Good Luck & Happy Coding.

here is d solution using case

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;
    break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
    count--;
    break;
  }
  
  if(count<=0){
     return (count + " Hold");
  }
  else{
      return (count + " Bet");
  }

1 Like