Last active
November 20, 2018 20:38
-
-
Save tuvo1106/fa5a1a01efa718f617689b5f64613e37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const example = [1,2,5,7,9,11,13,17,19] | |
const example2 = [-2,5,8,14,1,5,-9,-10,2] | |
avgWithReduce = function(arr) { | |
return arr.reduce(function(acc, number) { | |
// add accumulator to average of each number | |
return acc + number/arr.length | |
// set initial value to 0 | |
}, 0) | |
} | |
console.log(avgWithReduce(example)) // 9.333 | |
console.log(avgWithReduce(example2)) // 1.555 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment