Files
30-seconds-of-code/snippets/arrayLcm.md
Selim Ajimi 4b0bc342c1 Fix typos in spaces
Fix typo to fit the context

Fix typo add space

Fix small typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Update tail.md

Fix typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix small typo

Fix typo

Fix small typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo

Fix typo
2017-12-22 23:48:42 +01:00

433 B

arrayLcm

Calculates the lowest common multiple (lcm) of an array of numbers.

Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.

const arrayLcm = arr =>{
  const gcd = (x, y) => !y ? x : gcd(y, x % y);
  const lcm = (x, y) => (x*y)/gcd(x, y) 
  return arr.reduce((a,b) => lcm(a,b));
}
// arrayLcm([1,2,3,4,5]) -> 60
// arrayLcm([4,8,12]) -> 24