Created
December 7, 2015 22:05
-
-
Save codebubb/cd230970985c641be184 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: @codebubb | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-smallest-common-multiple# | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function smallestCommons(arr) { | |
| var nums = range(arr[0], arr[1]); | |
| return nums.reduce(lcm); | |
| } | |
| function gcd(a,b){ | |
| return b === 0 ? a : gcd(b, a % b); | |
| } | |
| function lcm(a,b){ | |
| return a * b / gcd(a,b); | |
| } | |
| function range(a,b){ | |
| var arr = []; | |
| if(a > b) b = [a, a = b][0]; | |
| for(; a<=b; a++) arr.push(a); | |
| return arr; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment