Push On Shift Off, Running Average
by mmansion
HTML
<input type="text"><button>Add</button>
<p>Average = <span id="out">0</span></p>
<pre></pre>
JavaScript
var sample = [];
var subset = 3;
var average;
$('button').click(function() {
var sum = 0;
sample.push($('input').val());
if(sample.length > subset) {
sample.shift();
}
$('pre').empty();
sample.forEach(function(value, index) {
sum += parseInt(value);
$('pre').append(value + '\n');
});
average = sum/sample.length;
$('#out').text(average);
});