diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/index.md index fc6ca1a6bc..4dc6db1701 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/index.md @@ -153,45 +153,25 @@ Note: If the array only has two elements, then the `for` loop never gets used an ## ![:rotating_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/rotating_light.png?v=3 ":rotating_light:") Advanced Code Solution: - function smallestCommons(arr) { + function smallestCommons(arr) { + // Euclidean algorithm for the greatest common divisor. + // ref: https://en.wikipedia.org/wiki/Euclidean_algorithm + const gcd = (a, b) => b === 0 ? a : gcd(b, a % b); - // range - let min = Math.min.apply(null, arr); - let max = Math.max.apply(null, arr); + // Least Common Multiple for two numbers based on GCD + const lcm = (a, b) => (a * b) / gcd(a, b); - let smallestCommon = lcm(min, min + 1); - - while(min < max) { - min++; - smallestCommon = lcm(smallestCommon, min); - } - - return smallestCommon; - } - - /** - * Calculates Greatest Common Divisor - * of two nubers using Euclidean algorithm - * https://en.wikipedia.org/wiki/Euclidean_algorithm - */ - function gcd(a, b) { - while (b > 0) { - let tmp = a; - a = b; - b = tmp % b; - } - return a; - } - - /** - * Calculates Least Common Multiple - * for two numbers utilising GCD - */ - function lcm(a, b) { - return (a * b / gcd(a, b)); - } + // range + let [min, max] = arr.sort((a, b)=> a - b); + let currentLCM = min; + while (min < max) { + currentLCM = lcm(currentLCM, ++min); + } + return currentLCM; + }; + // test here smallestCommons([1,5]); @@ -199,13 +179,13 @@ Note: If the array only has two elements, then the `for` loop never gets used an ### Code Explanation: -* Extract minimum and maximum from provided **arr**. +* Extract minimum and maximum from provided **arr** by sorting and grabbing the first and last values. * Initialise **smallestCommon** with the LCM of first two numbers. * Loop through range computing LCM of current LCM and next number in range **lcm(a, b, c) = lcm(lcm(a, b), c)**. #### Relevant Links -* JS Function.prototype.apply() +* Prefix increment operator ++ ## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS: