Card Counting, declare different var doesn't work

Hi!

I have a question about theCounting Card JS code:

First a made this code. But it doesn’t work, but if i change cardCount to count it does work. I thought i could use var++ on every variable (MDN). Does anyone know why my code doesn’t work as i just want to understand what i’m doing wrong? Thanks!

var count = 0;

function cc(card) {
  // Only change code below this line
 
 var cardCount =0;
    if(card <= 6){
    cardCount++;
  }
  else if( (card >= 10) || isNaN(card) ){
    cardCount--;
          }
  if(cardCount <= 0){
    return cardCount + " Hold";
  }
  else {
    return cardCount + " Bet";
  }
 // Only change code above this line
}
cc(2); cc(3); cc(4); cc(5); cc(6);

cardCount is reset to zero every time the function is called.

1 Like