Created
March 16, 2016 05:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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