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

后端 未结 11 584
野性不改
野性不改 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 15:59

    let arr = [8978, 'lol', -78, 989, NaN, null, undefined, 6, 9, 55, 989];
    
    
    let minMax = arr.reduce(([min, max], v) => [
                    Math.min(min, v) || min,
                    Math.max(max, v) || max], [Infinity, -Infinity]);
    
    
    console.log(minMax);

    How it works:

    1. || min check is v number.

    2. [Infinity, -Infinity] is .reduce initial value

    3. It use js destructuring assignment

提交回复
热议问题