Rolling average avg

by David Santiago

JavaScript

const numbers = [0,1,2,3,4,5,6,7,8,9,10];
let sum = 0;
numbers.map(el => sum+=el);
const avg = sum/numbers.length;

console.log("AVG:", avg);

let fixedRollingAvg = 0;
for (const num of numbers){
    fixedRollingAvg -= fixedRollingAvg / numbers.length;
    fixedRollingAvg += num / numbers.length;
}

console.log("Fixed Rolling", fixedRollingAvg);

let realRollingAvg = 0;
let counter = 1;
for (const num of numbers){
    realRollingAvg -= realRollingAvg / counter;
    realRollingAvg += num / counter++;
}

console.log("Real Rolling", realRollingAvg);



function approxRollingAverage (avg,new_sample) {
    avg -= avg / N;
    avg += new_sample / N;

    return avg;
}