0

**Have updated to put count and function in.

My question is that do equations when the return expression is put in, save that value so if that function is called again, it saves the previous value and adds on top of it? And I guess can someone elaborate on a higher level why/how this is?

(i.e.: console.log ((cc(3)) = 1 Bet)); console.log (cc(2)) = 2 Bet, instead of 1 Bet (count from cc(3) was "saved"))

var count = 0;
function cc(card) {
  // Only change code below this line
if (card>=2 && card<=6) {
    count+= 1; 
} else if(card>=7 && card<=9) {

} else {
    count-=1;      
}

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

}

4 Answers 4

2

My question is that do equations when the return expression is put in, save that value so if that function is called again, it saves the previous value and adds on top of it?

No. Equations don't save anything by themselves.

Variables declared within a scope retain their value for some period of time (depending upon the scope they are declared in and whether they are part of any sort of closure). So an equation that references a variable will always use the latest value of that variable. And a function that modifies a variable that is later used in an equation will cause the equation to see a new updated value.

EDIT

Since you now show that card is a variable in a higher scope that your cc() function, we can now explain that card will retain its value from one call to cc() to the next.

End of Edit

In the code you show, the current value of card is used anytime your code runs. You don't show where that variable is declared or how its value is set so we can't comment on that. When your code runs, it will use the current value of the card variable.

Likewise in the code you show, the current value of count will be used, updated and returned. Whether that modified value remains for the next time this code is run depends entirely upon how/where count is declared. You would need to show us more of your code including where count and card are declared and where else their values are set for us to comment further on exactly what happens with them.

Argument to a Function

If you do this:

function increment(item) {
    return ++item;
}

var cntr = 0;
console.log(increment(cntr));
console.log(increment(cntr));

You will get output that looks like this:

1
1

Because the value of cntr was never changed so each time you call increment() you were essentially doing increment(0) and nothing ever modifies the value of cntr so it stays at 0.

Assign Back Result to Original Variable

If, on the other hand, you did this:

function increment(item) {
    return ++item;
}

var cntr = 0;
cntr = increment(cntr);
console.log(cntr);
cntr = increment(cntr);
console.log(cntr);

Then, you would see this:

1
2

That's because you are assigning the return value from the function back to cntr so it's value is updated each time. So, the first time you call increment(), your doing increment(0) and the next time you're doing increment(1).


Local Variables

Local variables within a function exist only for the scope of that function. So, if you did this:

function increment() {
    var val = 0;
    return ++val;
}

console.log(increment());
console.log(increment());

You would see this:

1
1

Each invocation of increment() creates a new local variable val and initializes it to 0. So, everytime you call increment it is just going to get the value of 0 and increment it by one and return that so it will always return 1.


Higher Scope Variables

If, however, the variable val was in a higher scope:

var val = 0;

function increment() {
    return ++val;
}

console.log(increment());
console.log(increment());

Then, you would see this:

1
2

That's because the variable val is in a higher scope and the same val variable exists for both of your calls to increment() so it's value is carried from one call to the next.

Sign up to request clarification or add additional context in comments.

4 Comments

But one can make a local variable last across calls with closures
@JuanMendes - Yes you can. That is likely much more advanced that the OP is asking about though and we see no sign that closures are involved in the OP's question.
I have updated the question so that the variable + function + parameters were put in. Based on your last example, this explains why the var count = 0 was updated. Thank you for commenting such a detailed post. Had difficulty finding a concise, "higher" level explanation, but this was it.
@rich22 - OK, glad I finally covered the specific situation you wanted to know about.
1

Because you should declared the count variable outside of the function, not inside the function.If you want to print as you desire, put the count variable inside the function.

Comments

0

Try it :

<html>
    <head>
        <meta charset="utf-8">
        <style>
        </style>
    </head>
    <body>
        <script>
           function cc(card) {
               var count = 0;
               if (card>=2 && card<=6) {
                    count+= 1; 
                } else if(card>=7 && card<=9) {

                } else {
                    count-=1;      
                }

                if (count>0) {
                    return count + " Bet";
                } else {
                    return count + " Hold";
                }
                           }
            console.log(cc(3));
            console.log(cc(2));
        </script>
     </body>
</html>

Comments

0

I think you want to use a closure to store a variable that lasts across calls.

var cc = (function (){
    var count = 0;
    return function(value) {
        count += value;
        return count;
    }
})();

cc(2) // 2
cc(3) // 5

This is also known as the module pattern, returning a function instead of an object.

The easy way out would be to make count global, but doing it with the iife makes it so that no other code has access to that variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.