two biggest values in array

by Igor Cuckovic

JavaScript

function twoOldestAges(ages) {
    var second = ages.sort(function (a, b) {
        return b - a;
    })[1];
    ages = ages.filter(function (a) {
        return a >= second;
    });
    return ages.sort(function (a, b) {
        return a - b;
    });
}


console.log(twoOldestAges([1, 2, 10, 8])); // should return [8, 10]