-1

can anyone figure out what this possibly means?

Below code is to sum all the arguments, and I called the function like 'addTogether(2)(3)' then it works....?

For my understanding, calling a function should look like this 'addTogether(2, 3)'. put all the arguments in a pair of parentheses not two pairs of parentheses??

Could anyone explain why it worked and how it works?

I did console.log to figure this out, If I console.log(arguments) the result would be { '0': 2 }

function addTogether() {
  console.log(arguments) // result { '0': 2 }
  var args = Array.from(arguments);
  console.log(args) //result [2]
  
  return args.some(n => typeof n !== "number")
    ? undefined
    : args.length > 1
    ? args.reduce((acc, n) => (acc += n), 0)
    : n => (typeof n === "number" ? n + args[0] : undefined);
}

// test here
console.log(addTogether(2)(3)); // result 5
Kimovi
  • 35
  • 6
  • `addTogether(2)` returns a function, you're calling that function with `3`. – jonrsharpe Jan 27 '21 at 09:09
  • Relevant: [Two sets of parentheses after function call](https://stackoverflow.com/q/18234491) – VLAZ Jan 27 '21 at 09:12
  • @jonrsharpe hmm? then addTogether(2) is now [2], then it calls it again with (3)? like addTogether([2], 3) like this? – Kimovi Jan 27 '21 at 09:12

1 Answers1

-1
function addTogether(args) { 
  console.log(args)
  
  return args.some(n => typeof n !== "number")
    ? undefined
    : args.length > 1
    ? args.reduce((acc, n) => (acc += n), 0)
    : n => (typeof n === "number" ? n + args[0] : undefined);
}

Then call it like addTogether([1, 2])

[] Is a array, everything in it, seperated with a comma, is an item in the array. You can only have the same datatype in the array. In the (), where the function is defined, is where you should define what data you want to get, when the function is called.

If you want to test it quick, copy and paste it into the developer console in your browser, and call the function after