FCC Intermediate Algorithm Scripting: Arguments Optional

Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
For example, addTogether(2, 3)should return 5, and addTogether(2)should return a function.
Calling this returned function with a single argument will then return the sum:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3)returns 5.
If either argument isn't a valid number, return undefined.

function addTogether() {

if (Number.isInteger(arguments[0]) === false || (arguments[1] && Number.isInteger(arguments[1]) === false)) {
return undefined;
}
else if (arguments.length === 1) {
let arg1 = arguments[0];

return function(arg2) {
if (Number.isInteger(arg1) === false || Number.isInteger(arg2) === false) {
return undefined;
}
return arg1 + arg2;
}
}
return arguments[0] + arguments[1];
}

addTogether(2,3);

Comments

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int