Skip to content

Instantly share code, notes, and snippets.

@felquis
Created June 17, 2017 01:55
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 felquis/fb77b1ab9a532d4c04884a159d259b42 to your computer and use it in GitHub Desktop.
Save felquis/fb77b1ab9a532d4c04884a159d259b42 to your computer and use it in GitHub Desktop.
Take the average number of a x list of numbers in a sequence
export default function average (limit) {
let list = []
let current = 0
return (x) => {
list = [...list, x]
if (list.length < limit) return current
current = list.reduce((acc, x, index) => {
if (index === limit - 1) {
return acc / limit
}
return acc + x
}, 0)
list = []
return current
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment