Why is var count not resetting to 0 after every run

Tell us what’s happening:

Your code so far


var count = 0;

function cc(card) {
if (card >= 2 && card <= 6){
    count+=1;    }
else if (card >= 7 && card <=9){
    // return count + " Hold"
  }
else {
    count -=1
  }

if (count > 0) {      
return count + " Bet"}    
else if (count <= 0) {      
return count + " Hold"}
  
}

cc(2); cc(3); cc(4); cc(5); cc(6);
console.log(cc(6))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

the variable count is declared outside the function, it is a global variable, so it exists outside the function, as such it is one variable changed at each function run

Thanks ieahleen for the reply.
Its not getting reset to 0, when i call cc(2) , count should be 1
when i call cc(3), count should still be 1 (atleast thats what i assumed that count being declared outside will start from 0 again)…but it is adding up to 2

Thats why I am confused

Hello~!

That is the purpose of the function. In Blackjack, “counting cards” is a method used to gain an advantage by predicting the chance of getting a “strong” hand. When counting cards, I need to know how many low cards have been dealt from the deck and how many high cards have been dealt from the deck. By tracking this, I can guess the likelihood of getting two high cards (putting me close to 21) and bet accordingly. The function you are writing will do this for me.

In order to accomplish this, the function uses the count variable. When a low card is dealt, the count variable increases. When a high card is dealt, the count variable decreases. The function then tells me the count, and tells me if I should bet or hold. If count reset every time the function was called, then it would only be looking at the current card and not the history of cards dealt.

The variable is declared outside of the function, so you do not have a new variable starting from zero each time the function is called, no, you have the same variable that is changed by all the function calls you do

so cc(2) bring the count variable from 0 to 1, and then cc(3) bring the count variable from 1 to 2