Files
30-seconds-of-code/snippets/arrayGcd.md
Angelos Chalaris 97c250bcfb Updated examples
2017-12-27 16:35:25 +02:00

18 lines
406 B
Markdown

### arrayGcd
Calculates the greatest common denominator (gcd) of an array of numbers.
Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the greatest common denominator of an array of numbers.
```js
const arrayGcd = arr => {
const gcd = (x, y) => !y ? x : gcd(y, x % y);
return arr.reduce((a,b) => gcd(a,b));
}
```
```js
arrayGcd([1,2,3,4,5]) // 1
arrayGcd([4,8,12]) // 4
```