Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jorgepinon/e508330d409d08193231f75c341f7942 to your computer and use it in GitHub Desktop.
Save jorgepinon/e508330d409d08193231f75c341f7942 to your computer and use it in GitHub Desktop.
largestLessThanMaxLimit: higher-order function for reducing arrays to largest value while not going over the max defined
function largestLessThanMaxLimit(limit = 10) {
var maxLimit = limit;
return function largestLessThanMaxLimit(...args) {
return args.reduce( function(acc, val) {
if( val > acc && val < maxLimit ) {
return val;
}
else {
return acc;
}
}, args[0])
}
}
/*
usage:
p = largestLessThanMaxLimit(); // default limit, set to 10
p(1, 4, 6, 12) // 6
p = largestLessThanMaxLimit(5);
p(1, 4, 6, 12) // 4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment