I'm trying to write a function that used reduce() method to find the min and max value in the array, and then returns an array of size 2, where the first element is the min value, and the second element is the max value.
For example, for [3, 2, 1, 4, 5, 6, 7], my function needs to return [1,7].
I have the following code for now.
function minMax(items) {
return items.reduce(function(prev, curr, index){
let max = Math.max(prev, curr);
let min = Math.min(prev, curr);
return [min, max];
});
}
I have tested it, and it returns the min value or the max value just fine. But when I try to return the array, I get [NaN, NaN] as my result. I'm having trouble seeing where I am messing up. Also, yes, I do need to use reduce(), even though there are other ways of doing this same problem.
Please let me know how I can fix it. Thanks!