Manipulate pure javascript function to handle arguments.
I would like to get a function named "add" which is able to store and add its arguments (and obviously print it). Simple right ?
Steps :
- Create your add function to print its arguments.
- I have a really short memory so I need to be able to log in the console the sum of all of the arguments.
- Although I take my pills everyday, I sometimes make some mistakes and I have to reset my counter.
- We would be happy if our great function could take some arguments like that : add(1,2,3)..
- But we'd be very happy if you also could take some arguments like that too : add(1)(2,4)(3)
Bonus:
- Print the counter like that : add(4,7)(); // 11
- ..And like that : add.reset()(1)(2)(3)()(2,4,5)(2)(); // 6 and 19
- Finally, create the entire logic without global functions/variables (all you need is add !)
Now you should be able to have these results :
add(4);
add.toString(); // 4
add.reset();
add.toString(); // 0
add(4,7)(); // 11
add(1)(2)(3)(); // 17
add.reset()(2)(); // 2
add.reset()(1)(2)(3)()(2,4,5)(2)(); // 19