Error in counting cards don't know how to do


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

First of all, you need a space between the count and "Bet" or "Hold". You are correctly changing count, but not the "Bet" or "Hold".

Currently you decide to return "Bet" or "Hold" based on the last input. But it should be determined by the value of count. So you should remove the return from the current if .. else statement and create a new if ... else statement checking if count is larger than zero ("Bet"), or equal or smaller than zero ("Hold").

And since the else if(card==7||card==8||card==9) doesn’t change count, you can remove it.

Not sure if you just did not include it when you pasted your code in, but you also need to declare a global variable (outside of the function) named count and assign it an applicable value.

I was refactoring the Counting Cards challenge today and saw the perfect opportunity to eliminate the global count variable using closure.

var cc = (function() {
  var count = 0;
  return function(card) {
    if (card <= 6) count++;
    else if (/[1JQKA]/.test(card)) count--;
    return count + (count > 0 ? ' Bet' : ' Hold');
  }      
})();
1 Like

return count + (count > 0 ? ’ Bet’ : ’ Hold’);

I don’t understand the syntax use of ‘?’ and ‘:’. Where can I find this in the resource?