-2

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?

Miguel Jiménez
  • 1,276
  • 1
  • 16
  • 24

2 Answers2

0

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);
});
qwertynl
  • 3,912
  • 1
  • 21
  • 43
0

You can do this :

var r = foo(foo(0, function(){ foo = function(){ return 8 } }));

This gives to r the returned value which is 8.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758