DEV Community

Discussion on: What s wrong with Array.reduce ?

Collapse
 
icyjoseph profile image
Joseph

Calculating lcm of more than two numbers also becomes impressively simple and beautiful. For example my AoC day 12 solution.

function gcd(a, b) {
  if (!b) return b === 0 ? a : NaN;
  return gcd(b, a % b);
}

function lcm(a, b) {
  return (a / gcd(a, b)) * b;
}
const systemPeriod = periods.reduce(lcm)
Thread Thread
 
wulymammoth profile image
David

☝️