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

后端 未结 11 622
野性不改
野性不改 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: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;
    }
    

提交回复
热议问题