Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

max.js 978B

12345678910111213141516171819202122232425262728293031
  1. var _cb = require('./_cb.js');
  2. var _isArrayLike = require('./_isArrayLike.js');
  3. var each = require('./each.js');
  4. var values = require('./values.js');
  5. // Return the maximum element (or element-based computation).
  6. function max(obj, iteratee, context) {
  7. var result = -Infinity, lastComputed = -Infinity,
  8. value, computed;
  9. if (iteratee == null || typeof iteratee == 'number' && typeof obj[0] != 'object' && obj != null) {
  10. obj = _isArrayLike(obj) ? obj : values(obj);
  11. for (var i = 0, length = obj.length; i < length; i++) {
  12. value = obj[i];
  13. if (value != null && value > result) {
  14. result = value;
  15. }
  16. }
  17. } else {
  18. iteratee = _cb(iteratee, context);
  19. each(obj, function(v, index, list) {
  20. computed = iteratee(v, index, list);
  21. if (computed > lastComputed || computed === -Infinity && result === -Infinity) {
  22. result = v;
  23. lastComputed = computed;
  24. }
  25. });
  26. }
  27. return result;
  28. }
  29. module.exports = max;