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);
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
alert implicitly converts its argument to string for display. Read the above carefully, and try running it (that snippet is runnable).