Last active
August 29, 2015 13:56
-
-
Save bradoyler/9160984 to your computer and use it in GitHub Desktop.
JS console lesson : manipulating numeric arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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