srol
v2.0.0
Published
Class maintaining a rolling average.
Downloads
5
Maintainers
Readme
srol
Maintaining a simple dynamic rolling average.
The rolling average is the unweighted mean of the previous i numbers where i is the length or argument of the rolling average.
Install
Install with npm:
$ npm install --save srol
Usage
let srol = require('srol');
To create a rolling average of length n use:
let rollingAverage = new srol(n);
Methods
add
To add a number, or an array of numbers, to the rolling average use:
rollingAverage.add(n);
add will throw a TypeError if n is not a number or an array containing not a number.
average
Getter that returns the average of the last rollingAverage.length
numbers.
rollingAverage.average;
Example
Simple example or test of the rolling average.
let rollingAverage = new srol(3);
rollingAverage.add(5);
Since there is only 1 number in the array, the average will be 5.
rollingAverage.add(4);
rollingAverage.add(6);
Now the array is 'full' and the average will be 5, (4+5+6)/3.
rollingAverage.add(7);
The first number, 5, is replaced by a 7 and the average becomes 6.
> rollingAverage.average;
6
Running Tests
Test cover the basic function and requirements of the rolling average.
$ npm install && npm test
Contributing
Pull requests are always welcome.