1

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!

5 Answers 5

2

first of all, you don't pass anything to initialize reduce

secondly, prev is array of two elements and you have to use prev[0] and prev[1] if you want to get min and max

here is proper code:

function minMax(items) {
    return items.reduce(function(prev, curr, index){
       let max = Math.max(prev[1], curr);
       let min = Math.min(prev[0], curr);

        return [min, max];
    }, [Infinity, -Infinity]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is incorrect. Not sure why you get all the upvotes
check it now, I replaced indexes by mistake
0

You should pass an initial value like [Infinity, -Infinity]. And then use prev[0] or prev[1] to get the previous minimum or maximum.

function minMax(items) {
  return items.reduce(function(prev, curr, index) {
    let min = Math.min(prev[0], curr);
    let max = Math.max(prev[1], curr);
    return [min, max];
  }, [Infinity, -Infinity]);
}
console.log(minMax([3, 2, 1, 4, 5, 6, 7]));

Comments

0

Using the spread operator (...), you can turn your array of numbers into a set of arguments to pass to Math.min/Math.max.

var numbers = [3, 2, 1, 4, 5, 6, 7]

function minMax(items){
  let max = Math.max(...items)
  let min = Math.min(...items)
  
  return [min, max]
}

console.log(minMax(numbers))

2 Comments

OP mentioned that he needs to use reduce; besides, spread is part of ES6 and it is not widely supported; not to mention that you can achieve same result with simple apply
@pwolaqI appreciate you commenting and not just downvoting. Didn't notice the part about requiring reduce. And thanks for the info on spread also.
0

two problems. first problem is that your prev needs to be an array, second problem is that you don't have an initial value

function minMax(items) {
  var first = items[0];
  items.shift();
  if (items.length == 0) {
    return [first, first];
  }
  return items.reduce(function(prev, curr, index) {
    let min = Math.min(prev[0], curr);
    let max = Math.max(prev[1], curr);
    return [min, max];
  }, [first, first]);
}

3 Comments

Your code supposes items has at least one element.
it returns [undefined, undefined] if that is the case.
you may add if (items.length ==0) {error message} if you like.
0

You are making two mistakes:

  1. The pre that you get is an array [Min, Max], not a number.
  2. You need to initialize pre [Min, Max].

The following corrects both:

a = [3, 2, 1, 4, 5, 6, 7]
a.reduce(function(pre, cur, idx) { 
    return [Math.min(pre[0],cur), Math.max(pre[1],cur)]; 
}, [a[0], a[0]])
// Outputs [1, 7]

Explanation:

  • pre is determined by what value we return. Notice we are returning an array [Math.max(...), Math.min(...)] so pre is an array of exactly two elements. pre[0] denotes min and pre[1] denotes max.
  • cur is determined by the type of array elements. Since a is an array of numbers, cur is a number`

To further help you, here's how the above is working:

a = [3, 2, 1, 4, 5, 6, 7]
a.reduce(function(pre, cur, idx) { 
    var r = [Math.min(pre[0],cur), Math.max(pre[1],cur)]; 
    console.log("pre: ", pre, "cur: ", cur, "r: ", r)
    return r
}, [a[0], a[0]])

// Output
pre:  [3, 3] cur:  3 r:  [3, 3]
pre:  [3, 3] cur:  2 r:  [2, 3]
pre:  [2, 3] cur:  1 r:  [1, 3]
pre:  [1, 3] cur:  4 r:  [1, 4]
pre:  [1, 4] cur:  5 r:  [1, 5]
pre:  [1, 5] cur:  6 r:  [1, 6]
pre:  [1, 6] cur:  7 r:  [1, 7]
[1, 7]

3 Comments

I'm wondering why you sacrificed readability by removing almost all unnecessary whitespace characters, shortening variable names, yet didn't notice that idx is not used anywhere...
@pwolaq This is much more readable. I kept idx to not confuse OP.
Can you explain a bit more why I have to do pre[0] and pre[1]? Is pre not the value in the array but an array object? Does that mean only current is the value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.