Skip to content

Instantly share code, notes, and snippets.

@brettinternet
Last active January 30, 2017 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettinternet/ae109092f52a8cbe69113d03972dc5e6 to your computer and use it in GitHub Desktop.
Save brettinternet/ae109092f52a8cbe69113d03972dc5e6 to your computer and use it in GitHub Desktop.
Average every third element in an array for a more concise dataset
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
avgs = [],
sum = 0;
arr.forEach((el, i) => {
sum += el;
if ((i + 1) % 3 == 0) {
avgs.push(sum / 3);
sum = 0;
}
});
console.log(avgs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment