diff --git a/snippets/lcm.md b/snippets/lcm.md index e7bb6a3e0..4b560f29d 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -1,16 +1,15 @@ ### lcm -Returns the least common multiple of two or numbers/arrays. +Returns the least common multiple of two or more numbers/arrays. Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple. The GCD formula uses recursion. ```js const lcm = (...arr) => { - let data = [].concat(...arr); const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - const helperLcm = (x, y) => x * y / gcd(x, y); - return arr.reduce((a, b) => helperLcm(a, b)); + const _lcm = (x, y) => x * y / gcd(x, y); + return [].concat(...arr).reduce((a, b) => _lcm(a, b)); }; ```