Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2cf0f09a250e32eb8484 to your computer and use it in GitHub Desktop.
Save anonymous/2cf0f09a250e32eb8484 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Smallest Common Multiple
// Bonfire: Smallest Common Multiple
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-smallest-common-multiple
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function smallestCommons(arr) {
arr.sort(function(a, b) {
return b - a;
});
// Create new array and add all values from greater to smaller from the original array.
var newArr = [];
for (var i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
// Variables needed declared outside the loops.
var quot = 0;
var loop = 1;
var n;
while (n !== newArr.length){
quot = newArr[0] * loop * newArr[1];
for (n = 2; n < newArr.length; n++) {
if (quot % newArr[n] !== 0) {
break;
}
}
loop++;
}
return quot;
}
smallestCommons([1,5]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment