Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Created November 26, 2015 05:27
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 minsooshin/fd0d7cb3daadbe83398f to your computer and use it in GitHub Desktop.
Save minsooshin/fd0d7cb3daadbe83398f to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/minsooshin 's solution for Bonfire: Arguments Optional
// Bonfire: Arguments Optional
// Author: @minsooshin
// Challenge: http://www.freecodecamp.com/challenges/bonfire-arguments-optional
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function add() {
var args = Array.prototype.slice.call(arguments),
numberType = true;
args.forEach(function(val) {
if (typeof val !== 'number') {
numberType = false;
return;
}
});
if (numberType) {
if (args.length === 2) {
return args[0] + args[1];
} else {
return function() {
if (typeof arguments[0] !== 'number') return;
else {
return args[0] + arguments[0];
}
};
}
} else {
return;
}
}
add("http://bit.ly/IqT6zt"); //=> undefined
add(2, 3); //=> 5
add(2)(3); //=> 5
add(2, "3"); //=> undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment