Skip to content

Instantly share code, notes, and snippets.

@srkama
Created March 16, 2016 05:36
Show Gist options
  • Save srkama/645ad538f30b66b81534 to your computer and use it in GitHub Desktop.
Save srkama/645ad538f30b66b81534 to your computer and use it in GitHub Desktop.
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.
function add() {
function checkNum(num) {
if (typeof num != 'number') {
return false;
}
return true;
}
if(arguments.length == 1) {
var c=arguments[0];
if (checkNum(c)) {
return function(arg2) {
if(checkNum(arg2)) {
return c + arg2;
} else {
return undefined;
}
}
} else {
return undefined;
}
} else {
if(checkNum(arguments[0]) && checkNum(arguments[1])) {
return arguments[0] + arguments[1];
} else {
return undefined;
}
}
}
add(2,3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment