Link to home
Start Free TrialLog in
Avatar of ajinkya749
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>
Avatar of David Sankovsky
David Sankovsky
Flag of Israel image

++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.
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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial