Last active
January 30, 2017 21:19
-
-
Save brettinternet/ae109092f52a8cbe69113d03972dc5e6 to your computer and use it in GitHub Desktop.
Average every third element in an array for a more concise dataset
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
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