programming-examples/js/Functions/Write a JavaScript function which will take an array of numbers stored and find the second lowest and second greatest numbers, respectively..js
2019-11-15 12:59:38 +01:00

21 lines
491 B
JavaScript

function Second_Greatest_Lowest(arr_num)
{
arr_num.sort(function(x,y)
{
return x-y;
});
var uniqa = [arr_num[0]];
var result = [];
for(var j=1; j < arr_num.length; j++)
{
if(arr_num[j-1] !== arr_num[j])
{
uniqa.push(arr_num[j]);
}
}
result.push(uniqa[1],uniqa[uniqa.length-2]);
return result.join(',');
}
console.log(Second_Greatest_Lowest([1,2,3,4,5]));