0

I have been struggling to understand how to solve a problem below. I understand that it involves currying and I was able to come up with the answer if argument in sum() is included. However, I can't figure out how to solve it with sum() being empty.

I was able to come up with a solution for having a function invocation at the end s(1)(), but not for s(1).

var sum = function() { /* put your code here */};
var s = sum();

alert(s); // 0
alert(s(1)); // 1
alert(s(1)(2)); // 3
alert(s(3)(4)(5)); // 12

This is solution that works with all parameters being included and () at the end.

var sum = function(a){
   return function(b){
      if(b !== undefined){
          return sum(a+b);
      }
      return a;
    }
};

For the first s call console.log(s), I was only able to come up with simple solution below, to be able to return 0. It seems to be impossible to return 0 and then return a function such as s(1).

var sum = function(a){
  if(!arguments.length) return 0;
};

var s = sum();

console.log(s);
Roma Kim
  • 321
  • 1
  • 8
  • 1
    This is not possible. `s` cannot be a number and a function at the same time. – Bergi Sep 26 '22 at 12:37
  • "*I was able to come up with the answer*" - please post that code if you need help with it – Bergi Sep 26 '22 at 12:40
  • 1
    @mplungjan The edit radically changes this question. There is a solution for using `alert()` but it does not apply for `console.log()`. [See here](https://jsbin.com/nakasov/edit?js,output) – VLAZ Sep 26 '22 at 13:21
  • @Bergi possible with `alert()` since it implicitly calls `.toString()`, so it can be attached to the function to carry forward the result. – VLAZ Sep 26 '22 at 13:22
  • @VLAZ Oh, I missed the edit. But the duplicate still seems appropriate – Bergi Sep 26 '22 at 13:29
  • @Bergi mind if I reopen this? Or I can post [this](https://jsbin.com/koquhog/1/edit?js,console) under the linked duplicate with a disclaimer that it only works with `alert()` – VLAZ Sep 26 '22 at 13:31
  • 1
    @VLAZ I think the canonical duplicate is https://stackoverflow.com/questions/5832891/variadic-curried-sum-function - we don't need yet another question with answers – Bergi Sep 26 '22 at 13:33
  • @Bergi OK, thanks. I looked around for this but didn't find anything that talked about `toString()` and similar methods. – VLAZ Sep 26 '22 at 13:35
  • I've spent good 2 hours on this problem and it seems like there's no particular answer for this kind of solution. The biggest problem is being able to return just `s` with a value of 0. I can either return just `s` or the rest of the part, not together. – Roma Kim Sep 27 '22 at 01:40
  • @RomaKim Please [edit] your question to include the code you tried then. Notice it might be simpler to start with `function sum(n) { … } const s = sum(0);` – Bergi Sep 27 '22 at 01:42
  • 1
    @RomaKim You should use the `toString`/`valueOf` method on the function (as in the duplicate threads and VLAZ' jsbin), not add `()` everywhere. That'll also work for the initial function which should have a value of `0`. (If you were to add calls, you'd also want to use `s()` - your code already works for that if you just use `function sum(a=0)` or `if(!arguments.length) a = 0;`) – Bergi Sep 27 '22 at 02:00
  • Thank you guys for pointing me in right direction. This problem has been killing me for awhile. – Roma Kim Sep 27 '22 at 02:16

0 Answers0