Created
October 10, 2017 16:54
-
-
Save jwill9999/4e72e08845f705b1d86d67986cec4020 to your computer and use it in GitHub Desktop.
arguments Optional
This file contains 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
/* | |
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(num) { | |
num = Array.prototype.slice.call(arguments); | |
if (num.length > 1) { | |
return twoNumbers(num[0], num[1]); | |
} else { | |
return oneNumber(num[0]); | |
} | |
function isnumber(num) { | |
if (typeof num !== 'number' || typeof num === undefined) { | |
return undefined; | |
} else { | |
return num; | |
} | |
} | |
function twoNumbers(first, second) { | |
first = isnumber(first); | |
second = isnumber(second); | |
if (first !== undefined && second !== undefined) { | |
return first + second; | |
} | |
return undefined; | |
} | |
function oneNumber(num) { | |
num = isnumber(num); | |
if (num !== undefined) { | |
return function(num2) { | |
num2 = isnumber(num2); | |
if (num2 !== undefined) { | |
return num + num2; | |
} | |
return undefined; | |
}; | |
} | |
return undefined; | |
} | |
} | |
addTogether(4)([3]); | |
/* | |
Test your code here | |
addTogether(2, 3) should return 5. | |
addTogether(2)(3) should return 5. | |
addTogether("http://bit.ly/IqT6zt") should return undefined. | |
addTogether(2, "3") should return undefined. | |
addTogether(2)([3]) should return undefined. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment