Tell us what’s happening:
why is this part the ‘n’ is the second argument and not the first?
:n => (typeof n === ‘number’ ? n+args[0] : undefined);
for the better undestanding my question, i am asking why is that ‘n’ when i console it out, it gives me 7 not 5
//to see what i am saying
:n => (typeof n === ‘number’ ? console.log(n) : undefined);
shouldn´t it console 5 first then 7? because 5 is in front of 7
Your code so far
function addTogether() {
var args = Array.from(arguments);
console.log(args)
return args.some(n => typeof n !== 'number')
? undefined
: console.log(args.length > 1)
? args.reduce((a, b) => a+b, 0)
:n => (typeof n === 'number' ? n+args[0] : undefined);
}
console.log(addTogether(5)(7));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.
Because you’re just console logging the 7, you’ve explicitly written it so that if the function addTogether is passed a single number, it returns a function. If that function is passed a number, it console logs the number.
If you fix that, the logic still has issues. As there are only supposed to be a maximum of two arguments, reduce is pointless, you can just use +. The reason for using it is if you want to accept any number of arguments, ie if you wanted it to be more general than the instructions specify. But is so, your logic is still out. You only check if the function has a number as at least one of its arguments, and if it does you try to add. So for example addTogether(1, false, null, []) would attempt to return 1 + false + null + [].
As an aside, this code is incredibly difficult to read. Writing code as compactly as possible should not be an aim, you should strive for clarity, nested ternaries in no way clear