Posted By

Leech on 07/21/06


Tagged

arrays


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jkochis
nicolaspar


Average v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/array/average

Calculates the mean, standard deviation and variance in an array. Created: 2005.12.24

  1. /*
  2. **************************************
  3. * average Function v1.0 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7. average = function(a){
  8. var r = {mean: 0, variance: 0, deviation: 0}, t = a.length;
  9. for(var m, s = 0, l = t; l--; s += a[l]);
  10. for(m = r.mean = s / t, l = t, s = 0; l--; s += Math.pow(a[l] - m, 2));
  11. return r.deviation = Math.sqrt(r.variance = s / t), r;
  12. }

Report this snippet  

You need to login to post a comment.