# Please explain the following code & how the value of final_answer is evaluated?

<script>
function weird(x) {

var tmp = 3;

return function(y) {
return x + y + ++tmp;
}

}

var funny = weird(4);

var final_answer = funny(15);

alert(final_answer);  // the value of final_answer is 23//
</script>
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Senior SysAdminCommented:
++tmp is esetially tmp=tmp+1.

Meaning that the final cal is 15 (Sent from final_answer) + 4 (Sent from funny) + (3+1)
So 15+4+3+1 = 23.
0
Commented:
Well, the way I read this is that the variable called funny is actually a function where you have passed in 15, but the initial weird passes in 4 and tmp = 3 but ++tmp = 4 so the result when you pull the final answer is 15+4+4
0
Commented:
The code has a few blind corners that need addressing

1. The function weird returns a function (lambda) - that means that any variable assigned to the return value of the weird() function is itself a function
2. The function returned by the weird() call takes a parameter y
3. The expression in the returned function refers to the wierd(x) parameter the lambda function(y) parameter and the local variable tmp (local to weird).
4. The lambda pre-increments the tmp variable. [++tmp]. Pre-increment means the variable is incremented before it is used as opposed to post-incremented which is first used then incremented. Consider the following example
``````var x = 1;
var y = 2;
var z = x + ++y;
``````
In the above y is pre-incremented that means it is increased to 3 (2+1) before being added to x giving an answer of 4
On the other hand in the following example y is post incremented in other words its current value of 3 (assuming we are following on from the above code) is added to x before being incremented giving an answer of 4 BUT with y now equal to 4 after the operation.
``````var q = x + y++;
``````

``````<script>
function weird(x) {
var tmp = 3;
// RETURN A FUNCTION
// THE FUNCTION USES THE VALUE PASSED TO weird - x
// THE LOCAL VALUE tmp (WHICH IS PRE-INCREMENTED) IS
// ADDED TO THE PARAMETER x AND y PASSED IN THE
// CALL TO THE RETURNED FUNCTION
return function(y)
return x + y + ++tmp;
}
}

// GET A FUNCTION THAT ACCEPTS A PARAMETER y
// AND WILL RETURN 4 (parameter passed to weird) + PARAMETER(y) + 4 (++tmp - tmp WAS 3 AND WAS PRE-INCREMENTED)
var funny = weird(4);

// CALL THE RETURNED FUNCTION ABOVE WITH VALUE 15
// (y=15)
var final_answer = funny(15);
// FINAL ANSWER IS THEREFORE
// 4 + 15 + 4 = 23
// FIRST 4: PASSED TO WEIRD IN THE CALL weird(4)
//15: PASSED TO THE RETURNED FUNCTION (funny(15)
// SECOND 4: THE RESULT OF THE PRE-INCREMENT ON tmp

alert(final_answer);  // the value of final_answer is 23//
</script>
``````

The example demonstrates the following concepts
1. Scoping - how variable values are accessed at different scope levels. Here we have the parameter scope (x and y parameters) local scope (tmp) where the latter is accessible both to weird() as well as the returned lambda returned from weird
2. Returning functions as values that can then be used as functions
3. Effect on calculations of pre-increment vs post-increment.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.