A Chain adding function

JavaScript

function add() {
  var args = [...arguments];
  function addReturn() {
    // Recursion passing back previous total and remaining args
    return add(...args, ...arguments);
  }

  addReturn.value = args.reduce((total, val) => total + val);
  addReturn.valueOf = () => { return addReturn.value };
  return addReturn;
}

Tags

  1. javascript (Private)
  2. 5-kyu (Private)
  3. codewars (Private)
  4. answer (Private)