The averaging function itself is not such interesting. This is a demo of chaining, «this» and duck typing magic. Function gets arbitrary number of arguments and returns a function that gets arbitrary number of arguments that… etc. Until cast into number or string is happened, then it returns the average value of all arguments supplied.
137 bytes between { and }.
var avg = function(){/** code in index.js **/};
var average = avg(1).apply(this,[2,3,4])(5,6)()(7);
alert(average); /* "4" as in average of 1, 2, 3, 4, 5, 6 and 7 */
you should probably be using
valueOf
instead oftoString
.