Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active August 29, 2015 13:56
Show Gist options
  • Save bradoyler/9160984 to your computer and use it in GitHub Desktop.
Save bradoyler/9160984 to your computer and use it in GitHub Desktop.
JS console lesson : manipulating numeric arrays
// return 2nd highest and lowest numbers
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,1,2,3,4,4,5,5])); // output: 2, 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment