Javascript curried sum function throws an error

Viewed 19

Writing a curried sum function. Why does this throw an error saying curriedSum is not a function when invoked many times in one line? However, if I uncomment the else statement, it works fine. Doesn't the program go into the last identical return-statement when if-condition is not satisfied, and returns the same thing?

 function curriedSum(arg){

  let numbers = [];

  
  function summing(nextNum){
    numbers.push(nextNum);

    if(numbers.length === arg){
      return numbers.reduce(function(accum, num){
        return accum+num;
      });
    }
      // else{
     // return summing;
   // }
  }

  return summing;
}





const sum = console.log(curriedSum(3)(2)(1)(7));
0 Answers
Related