Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Last active November 20, 2018 20:38
Show Gist options
  • Save tuvo1106/fa5a1a01efa718f617689b5f64613e37 to your computer and use it in GitHub Desktop.
Save tuvo1106/fa5a1a01efa718f617689b5f64613e37 to your computer and use it in GitHub Desktop.
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