Javascript Array get third greatest
function thirdGreates(arr){ arr = arr.sort();/* ww w. j a v a 2 s. c o m*/ //console.log(arr); for(var i = 0; i < arr.length; i++){ return arr[arr.length - 3] } } console.log(thirdGreates([5, 3, 7])); console.log(thirdGreates([5, 3, 7, 4])); console.log(thirdGreates([2, 3, 7, 4])); function ThirdGreatest(strArr) { var sorted = strArr.sort(function(a, b){ return b.length - a.length; }); //console.log(sorted); return sorted[2]; } console.log(ThirdGreatest(["coder","fyte","code"])); console.log(ThirdGreatest(["abc","defg","aaa","ddd"])); console.log(ThirdGreatest(["hello", "world", "after", "all"]));