0

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);

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

no , as a result i have variable as a string.Changing names doesnt help.
@EdwardRyzhewski: No, there are no strings at all in your code until alert implicitly converts its argument to string for display. Read the above carefully, and try running it (that snippet is runnable).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.