Edward Ryzhewski
Edward Ryzhewski

Reputation: 7

Lambda function in javascript

What s wrong with my code? I dont understand why its not working.Something gone wrong with my lambda function.

var a=5;
var b=6;
function lambda_test(a){

    return function(a){
        return a*a;
    };
}
var c=lambda_test(a);
window.alert(c);

Upvotes: 0

Views: 388

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074545

The a in your inner function is shadowing the a in your outer function (which is, in turn, shadowing the outermost one — that's a lot of different as for just a few lines of code!). Give the arguments different names.

You're also never calling the function you get back and store in c. Your call to lambda_test creates the function, but doesn't call it; you'd then do that by calling c:

function lambda_test(outer){

    return function(inner){
        return outer * inner;
    };
}
var c=lambda_test(5);
alert(c(6)); // 30

Upvotes: 2

Related Questions