Counting Cards ( local, global variable difference )

Tell us what’s happening:
when the variable position is global var count=0; it is no problem…
But !!!
I’d like to move the variable, because in case OOP …
when the variable position is local var count=0; …
How this problem to resolve?
anybody help me, please

Your code so far

var count = 0; // no problem.

function cc(card) {
  var count = 0; // it is problem.
  // Only change code below this line
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
  if(count >0){
    return (count +" "+ "Bet");
  } else {
    return (count +" "+ "Hold");
  // Only change code above this line
//   return count;

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');

Your browser information:

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

Link to the challenge:

I think there are only three ways to declare variables in JavaScript—var, const and let; in your case only var and let are appropriate, but they will always be scoped to the individual function calls so you still need a variable that is outside the scope of the function to keep track of the count.

I’m not familiar with OOP in other languages, but, if I’m not mistaken, I think what you are wanting to do is to force a pattern that’s not JavaScript onto JavaScript. Don’t forget that this is only a simple example—in more complex code you always have the option (and you probably would) to scope a variable away from global if that’s really a concern for you. I hope that helps!

Thank you.
Actually, I meant as result make different…

case global varible : ‘var count = 0’ it is not problem. But
case local variable : ‘var count=0’ …result value is different because initial and function scope is same.
but I’d like to move at local… so… is it possible code Or is this needlessness though?

The results is different because if the variable count is declared inside the function cc, the variable count is only scoped to each function call. Since the challenge uses multiple function calls to calculate the final count, as far as I know, declaring variable outside of the function cc is the only way to do it for this challenge (hopefully someone can confirm this).

Outside the context of this challenge, there are patterns that you can use to avoid declaring a global variable. The simplest one that I can think of is simply don’t rely on multiple function calls, and in that case you can do the following:

function cc() {
  const cards = Array.from(arguments);
  let count = 0;
  cards.forEach((card) => {
    switch(card) {
      // Handle the different cases like you would before
      // And change count accordingly

  if(count >0){
    return (count + ' ' + "Bet");
  } else {
    return (count + ' ' + "Hold");

cc(A, 2, Q, K, 10);

I hope that is closer to what you’re looking for! :smile:

You are correct. The FCC tests for this challenge require count to be a global variable for the actual tests to work. Below is the first test which tests if the card sequence 2, 3, 4, 5, 6 returns “5 Bet”. See how the global variable count is reset before the consecutive calls the the cc function.

(function() {
  count = 0;
  var out = cc(6);
  if (out === "5 Bet") {
    return true;
  return false;
1 Like