-1

Hello i have problem as a total beginner trying to learn javascript with how this code works

The way the code should works look like this(taken from website : https://javascript.info/ chapter : Function object , NFE)

Write function sum that would work like this:

sum(1)(2) == 3; // 1 + 2
sum(1)(2)(3) == 6; // 1 + 2 + 3
sum(5)(-1)(2) == 6
sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15

this is the solution :

function sum(a) {

let currentSum = a;

function f(b) {
    currentSum += b;
    return f;
}

f.toString = function() {
    return currentSum;
};

return f;
}

alert( sum(1)(2) ); // 3
alert( sum(5)(-1)(2) ); // 6
alert( sum(6)(-1)(-2)(-3) ); // 0
alert( sum(0)(1)(2)(3)(4)(5) ); // 15

i have now understood what currying is thanks to comment but my last problem is method of function f(b)

  f.toString = function() {
      return currentSum;
  };

what it does? it is just instead of alerting the whole syntax of the function alerting the currentSum and if yes why it cant be written in function(b) like this:

function f(b) {
    currentSum += b;
    return f;

    toString() {
        return currentSum;
    };
}
Dakuha
  • 19
  • 4
  • 2
    Use a debugger and step through the code line by line. See [How can I debug my JavaScript code?](https://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Heretic Monkey Jul 30 '19 at 20:33
  • You may want to do some research on *function currying*. – Tyler Roper Jul 30 '19 at 20:34
  • Possible duplicate of [What is 'Currying'?](https://stackoverflow.com/questions/36314/what-is-currying) –  Jul 30 '19 at 20:35
  • What exactly is your question? It sounds like you have trouble understanding the code and want us to help with that, but what exactly are you not understanding? (Best, start explaining the things that you *are* understanding). – Bergi Jul 30 '19 at 20:38
  • Have a look at https://stackoverflow.com/questions/5832891/javascript-sum-function, https://stackoverflow.com/questions/28927510/add-function-that-works-with-different-combinations-of-chaining-arguments, https://stackoverflow.com/questions/25578728/puzzle-js-function-that-returns-itself-until-there-are-no-arguments – Bergi Jul 30 '19 at 20:38
  • Actually this is a horrible solution, since it does not support partial application. [This is a much better approach](https://stackoverflow.com/a/18067040/1048572) :-) – Bergi Jul 30 '19 at 20:40
  • @Bergi I have added what im not understanding now, i understood what currying is tha last problem is written at the end of the question(just edited it) – Dakuha Jul 30 '19 at 20:52
  • @Bergi thank you so much, now i understand everything, you just saved me from frustration , again thank you for your time Bergi ,you are a very cool guy – Dakuha Jul 30 '19 at 21:01
  • OK, I guess I should've posted that as answer... – Bergi Jul 30 '19 at 21:06

1 Answers1

0

Is it just instead of alerting the whole syntax of the function alerting the currentSum

Yes, that's what this does. When faced with an object (and functions are objects), alert tries to convert it to a string, basically as if you had done alert(….toString()).

Why it cant be written in function(b) like this

Because that's a syntax error, this is not how you attach properties (methods) to functions. Even though functions are objects, that doesn't mean you can just put a method definition in the middle of its body as if it was an object literal.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375