Elegant way to calculate LCM and GCD from an array of values in Javascript ES6 and ES5

With ES6

const gcd = (a, b) => a ? gcd(b % a, a) : b;

const lcm = (a, b) => a * b / gcd(a, b);
Then use reduce on given array of integers:

[1, 2, 3, 4, 5].reduce(lcm); // Returns 60

With ES5

var gcd = function (a, b) {
    return a ? gcd(b % a, a) : b;
}
var lcm = function (a, b) {
    return a * b / gcd(a, b);
}
Then use reduce on given array of integers:
[1, 2, 3, 4, 5].reduce(lcm); // Returns 60

Source: stackoverflow 

Comments