| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 1 1 27 15 12 4 8 8 24 24 12 12 8 1 | define(['./forEach'], function (forEach) {
/**
* Return maximum value inside array
* @version 0.1.0 (2012/01/29)
*/
function max(arr, iterator){
if (arr.length && !iterator) {
return Math.max.apply(Math, arr);
} else if (!arr.length) {
return Infinity;
} else {
var result,
compare = -Infinity,
tmp;
forEach(arr, function(val, i, list){
tmp = iterator(val, i, list);
if (tmp > compare) {
compare = tmp;
result = val;
}
});
return result;
}
}
return max;
});
|