If we call sum function multiple time as given below sum(1) // return 1 sum(1)(2) // return 3 sum(1)(2)(3) // re | JS, TS, React and Angular Forum
J
JYOTI SHARMA Posted on 17/01/2022
  • sum(1) // return 1
  • sum(1)(2) // return 3
  • sum(1)(2)(3) // return 6
  • sum(1)(2)(3)(4) // return 10

Y
Yogesh Chawla Replied on 17/01/2022

Try this:

function sumAll(arr) {
  var out = 0;
  for (var i = Math.min(...arr); i <= Math.max(...arr); i++) {
    out += i;
  }
  return out;
}

console.log(sumAll([1, 4]));
console.log(sumAll([4, 1, 5]));
console.log(sumAll([5, 10, 15, 4]));
console.log(sumAll([10, 2]));

Using rest function parameters(or spread operator)