Roll Your own One-Line Math.average Function Using Array.reduce

Nick Scialli (he/him) - Apr 11 '20 - - Dev Community

Just a quick example of using array.reduce to roll your own one-liner average calculation!

const average = (...nums) => 
  nums.reduce((a, b) => a + b, 0) / nums.length;

console.log(average(1, 2, 3, 4));
// 2.5
Enter fullscreen mode Exit fullscreen mode

Cheers!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player