Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JustSamuel/srol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

17 Commits

Repository files navigation

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.

About

Simple rolling average in nodejs.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

AltStyle によって変換されたページ (->オリジナル) /