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
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:
|| min
check is v
number.
[Infinity, -Infinity]
is .reduce
initial value
It use js destructuring assignment