Skip to content

Instantly share code, notes, and snippets.

@platypusrex
Created October 15, 2015 04:15
Show Gist options
  • Save platypusrex/c4c7b7c232fae04f0541 to your computer and use it in GitHub Desktop.
Save platypusrex/c4c7b7c232fae04f0541 to your computer and use it in GitHub Desktop.
Javascript - Arguments and Closures
function add() {
if(arguments.length > 1){
return Array.from(arguments).every(function(val){
return typeof val === 'number';
}) ? Array.from(arguments).reduce(function(prev, current){
return prev + current
}) : undefined;
}else if(typeof arguments[0] === 'number'){
var x = arguments[0];
return function(y){
return typeof y === 'number' ? x + y : undefined;
}
}else {
return undefined
}
}
//all data types besides number will return undefined
//add('2') --> undefined
//add(2,'3') --> undefined
var addToAdd = add(2);
addToAdd(7); // closure example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment