Card Counting - JS Tips? [SOLVED]

Hi guys,

I’ve got a partial solution to this but I’m still missing two check boxes out of the seven I need - and I’ve no idea why this isn’t working.

Additionally, a comment for FCC - the question doesn’t make it clear at all that there’s already some background code for feeding in the inputs in sequence… I can’t imagine how many folk have become bogged down trying to figure out how to create a loop to update the global “count” value…! Perhaps some clarity is required in the question.

My code so far is below; any hints on why this isn’t giving me a solution?? :-

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 ++;  
      var decision = " Bet"; 
      break;
    
case 7:
 case 8:
case 9: 
        
decision = " Hold";
break;
        
    case 10:
    case 'J':
    case 'Q': 
    case 'K':
    case 'A':
        count --;  
      decision = " Hold"; 
      break;
    
    }
  return count + decision;

  // 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('Q');

After some more testing… the reason it wasn’t working was because I was assigning Hold to the last two case groups, even though the count could have finished on 1.

Still, I think if FCC gave some clarification that we don’t need to worry about the feeding in of the sequence, that’d be a great help.

For anyone else struggling, you just have to create a loop manually by imagining it in your head when you feed each sequence item into your function - you don’t have to write a function that will read the array at the bottom of the page - your code should only pay attention to the last entry.

My working code:-

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

FYI - You could get rid of the following lines, and your code will still work.

case 7:
 case 8:
case 9: 
        
break;
1 Like