Search code examples
javascriptfunctional-programmingchallenge-response

Javascript challenge - How to call a function such that it returns 8?


Long time ago I saw a Javascript challenge, I do not remember where, but now I am trying to solve it. Until now, I do not know how to proceed.

I have this function and I have to make a call such that it returns 8:

function foo(a, b){
    b(function(){
        return a + a;
    });
}

Since foo does not return anything, is it possible to make the call I need? or it is just a tricky question?


Solution

  • If by "return" you mean alert or log it somewhere it is easy as:

    foo(4, function(fn){
       console.log(fn());
    });
    

    Or even ignoring the input:

    foo('bar', function(){
       console.log(8);
    });