get the first and second highest values of an array using javascript - Stack Overflow

I am trying to perform the following code:function oldestAges(ages){if (ages == []){return [0,0]; }else

I am trying to perform the following code:

function oldestAges(ages){
    if (ages == []){
      return [0,0]; 
    }else{
      var max = Math.max.apply(null, ages); 
              ages.splice(ages.indexOf(max), 1); 
      var max2 = Math.max.apply(null, ages); 
      return [max2,max];
  }
}

However, when testing [] as ages, the expected was '[0, 0]', instead got: '[-Infinity, -Infinity]'

Also, is there a much easier way to acplish the same task? I ask because using R I could get the same result in much less number of lines. I am noob in javascript still.

I am trying to perform the following code:

function oldestAges(ages){
    if (ages == []){
      return [0,0]; 
    }else{
      var max = Math.max.apply(null, ages); 
              ages.splice(ages.indexOf(max), 1); 
      var max2 = Math.max.apply(null, ages); 
      return [max2,max];
  }
}

However, when testing [] as ages, the expected was '[0, 0]', instead got: '[-Infinity, -Infinity]'

Also, is there a much easier way to acplish the same task? I ask because using R I could get the same result in much less number of lines. I am noob in javascript still.

Share Improve this question edited Oct 1, 2018 at 20:32 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Oct 1, 2018 at 20:31 motipaimotipai 3283 silver badges12 bronze badges 4
  • 1 You can't use == to pare arrays by their contents. The operator pares only by object identity, so one array will never ever be == to another array. – Pointy Commented Oct 1, 2018 at 20:32
  • 2 Use if(ages.length == 0) to test for an empty array. – Barmar Commented Oct 1, 2018 at 20:33
  • The simple way is to sort the array and then return the first two elements. – Barmar Commented Oct 1, 2018 at 20:33
  • As @Barmar implied: the problem is in this line: if (ages == []){ - it will always return false – Nir Alfasi Commented Oct 1, 2018 at 20:50
Add a ment  | 

3 Answers 3

Reset to default 4

A simple (not sure about the most optimal) way to achieve this:

const input = [5, 150, 2, 8, 58, 4];

const result = input.sort((x, y) => y - x).slice(0, 2);

console.log(result);

You can sort your array in descending order so that the first two elements contain the highest values.

ages.sort((a, b) => b - a);

Now ages[0] and ages[1] contain the two biggest numbers.

ages.length == 0 is what you can use to see if the number of the elements in the array is zero.

Your code has an error where you say if (ages == []).

Try this,

function oldestAges(ages){
        if (ages.length == 0){
          return [0,0];
        }else{
          var max = Math.max.apply(null, ages);
                  ages.splice(ages.indexOf(max), 1);
          var max2 = Math.max.apply(null, ages);
          return [max2,max];
      }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745486685a4629798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信