30 seconds of code

node v8.17.0
version: 1.0.0
endpointsharetweet
### arrayAverage Returns the average of an array of numbers. Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
arrayAverage([1,2,3])
### 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.
const arrayGcd = arr =>{ const gcd = (x, y) => !y ? x : gcd(y, x % y); return arr.reduce((a,b) => gcd(a,b)); }
arrayGcd([1,2,3,4,5])
arrayGcd([4,8,12])
Loading…

no comments

    sign in to comment