Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Adds all the natural numbers below `end` that are multiples of any of the
// values in `multiples`
//
// end - The Number that is the upper limit
// multiples - An Array of Numbers that will be checked for divisibility
//
// Examples
//
// multiples(1000, [3, 5])
// // => 233168
//
// Returns the Number sum of the multiples (see description)
function multiples(end, multiples) {
// create an array of number starting at 0 and ending at `end`
var range = _.range(end);
// filter the array to only contain numbers that are multiples of the numbers in `multiples`
var filtered = _.filter(range, function(i) {
// return true if i is divisible by any of the numbers in multiples
return _.any(multiples, function(mult) {
// return true if i is divisible by `mult`
return i % mult === 0;
});
});
// sum the numbers and return the result
return _.reduce(filtered, function(a, b) {
return a + b;
});
}
console.assert( multiples(1000, [3, 5]) === 233168 );
console.log("multiples: Success");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.