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

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

    I know this has been answered but I went off of @Sergey Zhukov's answer (which seems incomplete) and was able to get the min and max values in 2 lines:

    let vals = [ numeric values ]
    let min = Math.min.apply(undefined, vals) 
    let max = Math.max.apply(undefined, vals)
    

    I do see the value in Array.reduce, but with such a super simple use case, and so long as you understand what Function.apply does, this would be my goto solution.

提交回复
热议问题