Skip to content

Instantly share code, notes, and snippets.

@eday69
Last active June 15, 2018 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eday69/169367e8f9b2a560399d238e0b09e5f1 to your computer and use it in GitHub Desktop.
Save eday69/169367e8f9b2a560399d238e0b09e5f1 to your computer and use it in GitHub Desktop.
freeCodeCamp 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() {
let returnValue;
if (typeof arguments[0] === "number") {
if (typeof arguments[1] === "undefined") {
let firstArg = arguments[0];
// var sumTwoAnd = addTogether(arguments[0]);
return function(secondVal) {
let returnValue2;
if (typeof secondVal === "number") {
returnValue2=firstArg + secondVal;
}
return returnValue2;
}
}
else if (typeof arguments[1] === "number") {
returnValue = arguments[0] + arguments[1];
}
}
return returnValue;
}
addTogether(2,3);
addTogether(2)(3); // 5.
addTogether("http://bit.ly/IqT6zt"); // undefined.
addTogether(2, "3"); // undefined.
addTogether(2)([3]); // undefined.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment