rich22 rich22 - 4 months ago 5
Javascript Question

How does JavaScript "save" values via return?

**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";
}


}

Answer

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.

Comments