A sum function with a twist in JavaScript // Rest operator, currying, promises
In this video we look at an interesting way to build a summation function in JavaScript that takes an unusual approach to passing arguments. We show how to write a sum function that returns another function instead of computing a result straight away. That lets us chain calls together.
The example is a sum function that takes a number n and returns another function capable of adding n to further numbers. We look at ways of feeding in additional numbers using inner functions.
We also cover two variations on the problem: the case where the number of calls is unbounded, and the case where the final call ends with empty parentheses. The solution we offer for that logic keeps you from writing verbose code.
You will learn how to start implementing such a function and which variables you need to hold the running total. A useful and genuinely interesting video for anyone who wants to stretch their JavaScript and get to grips with the rest operator, currying and promises.



