Created
June 17, 2017 01:55
-
-
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
This file contains hidden or 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
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