Skip to content

Instantly share code, notes, and snippets.

@ronaldoarg
Created March 28, 2018 01:43
Show Gist options
  • Save ronaldoarg/094edf64da0e9e905e035dccaa1960af to your computer and use it in GitHub Desktop.
Save ronaldoarg/094edf64da0e9e905e035dccaa1960af to your computer and use it in GitHub Desktop.
My solution for FreeCodeCamp Smallest Common Multiple Challenge
// https://www.freecodecamp.org/challenges/smallest-common-multiple
function smallestCommons(arr) {
var max = Math.max.apply(this, arr),
min = Math.min.apply(this, arr),
multiple = max,
numbers = [];
for(var count = min; count <= max; count++) {
numbers.push(count);
}
while(!isSCM(multiple, numbers)) {
multiple += max;
}
return multiple;
}
function isSCM(multiple, numbers) {
return !numbers.map(function(item) {
return (multiple % item == 0);
}).some(function(item) {
return item == false;
});
}
smallestCommons([23, 18]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment