Created
January 16, 2018 06:20
-
-
Save LSStaff/7322e3d2ed679d703047b09b9c91419d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumOfMultiples(targetNumber, factors) { | |
var multiples = []; | |
if (factors.length === 0) { | |
factors = [3, 5]; | |
} | |
factors.forEach(function(factor) { | |
var currentMultiple; | |
for (currentMultiple = factor; currentMultiple < targetNumber; currentMultiple += factor) { | |
if (multiples.indexOf(currentMultiple) === -1) { | |
multiples.push(currentMultiple); | |
} | |
} | |
}); | |
return multiples.reduce(function(sum, value) { | |
return sum + value; | |
}, 0); | |
} | |
sumOfMultiples(20, [3, 5]); // returns 78 | |
sumOfMultiples(20, [3]); // returns 63 | |
sumOfMultiples(20, [5]); // returns 30 | |
sumOfMultiples(20, []); // returns 78 | |
sumOfMultiples(1, []); // returns 0 | |
sumOfMultiples(20, [19]); // returns 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment