Running Map

Overview


Moving Average Examples
Moving Average Examples

function runningMovingAverage(size){
	let total = 0;
	let items = [];
	return function(item){
		total += item;
		items.push(item);
		if(items.length>size){
			let first = items.shift();
			total -=first;
		}
		if(items.length === size) return total/size;
		return null;
	}
}
				



let data = [1,2,3,4,5];
let results = data.map(function(item){
  return 2*item;					
});
				
Try it!

Contents