[removed] Using reduce() to find min and max values?

后端 未结 11 576
野性不改
野性不改 2020-12-09 15:46

I have this code for a class where I\'m supposed to use the reduce() method to find the min and max values in an array. However, we are required to use only a single call to

相关标签:
11条回答
  • 2020-12-09 16:07

    As the reduce call isn't really needed at all, you could have some fun with it

    let items = [62, 3, 7, 9, 33, 6, 322, 67, 853];
    
    let arr = items.reduce((w,o,r,k,s=Math)=>[s.min.apply(0, k),s.max.apply(0, k)],[]);
    
    console.log(arr);

    All you'd really need is let minMaxArray = [Math.min.apply(0,items), Math.max.apply(0,items)]

    0 讨论(0)
  • 2020-12-09 16:10
    const values = [1,2,3,4,5];
    const [first] = values;
    const maxValue = values.reduce((acc, value) => Math.max(acc, value), first);
    
    0 讨论(0)
  • 2020-12-09 16:10

    1. Solution using only Math.min and Math.max:

    ⚠️ This will not work if you use big arrays, i.e. supply Math.min() with many arguments as "you run the risk of exceeding the JavaScript engine's argument length limit. The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception." from MDN web docs.

    function minMax(items) {
      return [
          Math.min.apply(null, items),
          Math.max.apply(null, items)
      ]
    }
    

    ... or if you prefer ES6's Spread syntax:

    const minMax = items => [
      Math.min(...items),
      Math.max(...items)
    ]
    

    2. Solution using Array.prototype.reduce, Math.min and Math.max

    function minMax(arr) {
      return arr.reduce(function(acc, cur) {
        return [
          Math.min(cur, acc[0]),
          Math.max(cur, acc[1])
        ]
      }, [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]);
    }
    

    ... or shortened:

    const minMax = items =>
      items.reduce((acc, cur) =>
        [Math.min(cur, acc[0]), Math.max(cur, acc[1])],
        [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
      )
    
    

    3. Solution including sensible validations

    function minMax(items) {
      let newItems = []
      const isArray = Array.isArray(items)
      const onlyHasNumbers = !items.some(i => isNaN(parseFloat(i)))
    
      // only proceed if items is a non-empty array of numbers
      if (isArray && items.length > 0 && onlyHasNumbers) {
        newItems = items.reduce(function(acc, cur) {
          return [
            Math.min(cur, acc[0]),
            Math.max(cur, acc[1])
          ]
        }, [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])
      }
      return newItems
    }
    

    Documentation for Math.min

    Documentation for Math.max

    Documentation for Array.prototype.reduce()

    0 讨论(0)
  • 2020-12-09 16:10

    You can use do like this. There can be any number of arguments.

    function minValue(...args) {
        const min = args.reduce((acc, val) => {
            return acc < val ? acc : val;
        });
        return min;
    }
    
    function maxValue(...args) {
        const max= args.reduce((acc, val) => {
            return acc > val ? acc : val;
        });
        return max;
    }
    
    0 讨论(0)
  • 2020-12-09 16:11

    To get min and max value of an array using reduce function

    const ArrayList = [1, 2, 3, 4, 3, 20, 0];
    const LargestNum = ArrayList.reduce((prev, curr) => {
          return Math.max(prev, curr)
    });
    const MinNum = ArrayList.reduce((prev,curr)=>{
          return Math.min(prev,curr)
    });
    console.log(LargestNum);
    console.log(MinNum);
    
    0 讨论(0)
提交回复
热议问题