ajinkya749
asked on
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>
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>
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Meaning that the final cal is 15 (Sent from final_answer) + 4 (Sent from funny) + (3+1)
So 15+4+3+1 = 23.