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.
If either argument isn’t a valid number, return undefined.
Everything else passed but this one didn’t:
addTogether(5, undefined)`should return undefined.
My code:
function addTogether(a, b) {
if (typeof a === "number" && typeof b === "number"){
return a + b;
}
if (typeof a === "number" && b === undefined){
return function addB(b){
if (typeof b === "number"){
return a + b;
} else{
return undefined;
}
};
} else{
return undefined;
}
}
console.log(addTogether(2,3));