最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find smallest and biggest number of an array with reduce function in javascript - Stack Overflow

programmeradmin1浏览0评论

Just a matter of curiosity. With the reduce function, we could easily find the smallest and the biggest number inside an array separately. Just like that:

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];
a.reduce(function(prev,cur,index,array){
    return prev > cur ? prev : cur;
}); // returns 11

a.reduce(function(prev,cur,index,array){
    return prev < cur ? prev : cur;
}); // returns -1

Given that, why this don't work?

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];
var smallest = 0;
var biggest = 0;
a.reduce(function(prev,cur,index,array){
    smallest = prev < cur ? prev : cur;
    biggest =  prev > cur ? prev : cur;
});
console.log([smallest, biggest]); // prints [11,11]

Tested on repl.it.

Just a matter of curiosity. With the reduce function, we could easily find the smallest and the biggest number inside an array separately. Just like that:

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];
a.reduce(function(prev,cur,index,array){
    return prev > cur ? prev : cur;
}); // returns 11

a.reduce(function(prev,cur,index,array){
    return prev < cur ? prev : cur;
}); // returns -1

Given that, why this don't work?

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];
var smallest = 0;
var biggest = 0;
a.reduce(function(prev,cur,index,array){
    smallest = prev < cur ? prev : cur;
    biggest =  prev > cur ? prev : cur;
});
console.log([smallest, biggest]); // prints [11,11]

Tested on repl.it.

Share Improve this question edited Mar 16, 2018 at 10:30 radbyx 9,66022 gold badges90 silver badges133 bronze badges asked Sep 28, 2014 at 20:31 cezarlamanncezarlamann 1,5234 gold badges29 silver badges44 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 7

In the following:

a.reduce(function(prev,cur,index,array){
    smallest = prev < cur ? prev : cur;
    biggest =  prev > cur ? prev : cur;
});

the function supplied to reduce has no return statement so it returns undefined. So after the first iteration, prev is set to undefined.

If either expression in the abstract relational comparison algorithm is undefined, the expression returns undefined (see step 3.c), which evaluates to false. So from the second iteration onward, both smallest and biggest are set to cur and at the end they're both set to the last value in the array.

While using reduce, you need to return something inside reduce, otherwise reduce forgets previous values.

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];
var initial = {
  smallest: a[0],
  biggest: a[0]
};
var result = a.reduce((prev, cur) => {
  prev.smallest = prev.smallest < cur ? prev.smallest : cur;
  prev.biggest = prev.biggest > cur ? prev.biggest : cur;
  return prev;
}, initial);

console.log(result);
// Prints object as,
// { smallest: -1, biggest: 11 }

Two problems.

First, the lambda parameter to reduce has no return value. If you aren't going to return something, reduce is just forEach with more parameters that don't mean anything.

Second, at each element, you compare cur to prev instead of comparing cur to biggest and smallest.

I know this is ancient but here is how to solve such issues using array reduce:

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];

var minNumber = a.reduce(function(prev,cur) {
    return prev < cur ? prev : cur;
}, +Infinity);

var maxNumber = a.reduce(function(prev,cur) {
    return prev > cur ? prev : cur;
}, -Infinity);

Personally I'd just use Math.min/max:

var a = [1,2,3,4,5,1,-1,6,7,8,9,10,2,11];

Math.min(...a) // will give -1
Math.max(...a) // will give 11
发布评论

评论列表(0)

  1. 暂无评论