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

后端 未结 11 625
野性不改
野性不改 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:05

    You can use array as return value:

    function minMax(items) {
        return items.reduce(
            (accumulator, currentValue) => {
                return [
                    Math.min(currentValue, accumulator[0]), 
                    Math.max(currentValue, accumulator[1])
                ];
            }, [Number.MAX_VALUE, Number.MIN_VALUE]
        );
    }
    

提交回复
热议问题