Created
December 3, 2015 02:51
-
-
Save anonymous/2cf0f09a250e32eb8484 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Smallest Common Multiple
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
// 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