Posted By

EvanHahn on 03/17/12


Tagged

numbers array average


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

spee66


Average array


 / Published in: JavaScript
 

URL: http://evanhahn.com/?p=663

Basically, this creates a string expression of all of the array values added up (eg, 1+2+3) and then evaluates it with eval. Finally, it divides by the number of elements.

  1. var average = eval(arrayToAverage.join('+')) / arrayToAverage.length;

Report this snippet  

You need to login to post a comment.