Card Counting - var count = 0 inside function

Your code so far

var count = 0;
function cc(card) {

    switch(card){
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        count = count + 1
          break
      case 7:
      case 8:
      case 9:
        count = count
          break
      case 10:
      case 'J':
      case 'Q':
      case 'K':
      case 'A':
        count = count - 1
          break
}
  var holdbet = ""
    if(count > 0){
      holdbet = "Bet"
    }else{
      holdbet = "Hold"
    }

        return count + " " + holdbet
}

Tell us what’s happening:
I’m wondering if this var count = 0 before function can be placed inside the function like this:

function cc(card) {
var count = 0
    switch(card){
      case 2:
      case 3:

Would you mind trying to fix the markdown for this?
Make sure to use trip back-ticks ``` to open and close your code.
That should make it easier for others to help you with your issue!

1 Like

Have you tried placing the var count = 0 within the function yet? Sometimes it’s a good idea to explore these ideas yourself! This will help you gain a better understanding of how everything works.

If moving the var count = 0 into the function doesn’t work, try to find out why!

Yup I tried it, but firstly i didn’t know why it’s a bad way.
Now I think I know; when it’s outside - every time the function is ended it’s gonna add a value to the global count variable ; when it’s inside the function it will start counting from zero every time the function is called; so it’s useless
Is that right?

1 Like

correct

note that most often you want a function that doesn’t change values outside of it. In some cases a function that changes things in the outside scope is used, but it is not the default, and it would be wrong for it to be the default.

1 Like