max.js 869 B

12345678910111213141516171819202122232425262728293031323334
  1. var makeIterator = require('../function/makeIterator_');
  2. /**
  3. * Return maximum value inside array
  4. */
  5. function max(arr, iterator, thisObj){
  6. if (arr == null || !arr.length) {
  7. return Infinity;
  8. } else if (arr.length && !iterator) {
  9. return Math.max.apply(Math, arr);
  10. } else {
  11. iterator = makeIterator(iterator, thisObj);
  12. var result,
  13. compare = -Infinity,
  14. value,
  15. temp;
  16. var i = -1, len = arr.length;
  17. while (++i < len) {
  18. value = arr[i];
  19. temp = iterator(value, i, arr);
  20. if (temp > compare) {
  21. compare = temp;
  22. result = value;
  23. }
  24. }
  25. return result;
  26. }
  27. }
  28. module.exports = max;