Created
January 21, 2014 22:27
-
-
Save areagray/8549770 to your computer and use it in GitHub Desktop.
Coderbyte: Have the function ThirdGreatest(strArr) take the array of strings stored in strArr and return the third largest word within in. So for example: if strArr is ["hello", "world", "before", "all"] your output should be world because "before" is 6 letters long, and "hello" and "world" are both 5, but the output should be world because it a…
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
//Coderbyte | |
function ThirdGreatest(strArr) { | |
var matrix=[]; | |
//build the array, two dimensions | |
for (var i=0; i<strArr.length;i++){ | |
matrix.push([strArr[i].length,strArr[i]]); | |
} | |
//sort | |
matrix.sort(function(a,b){ | |
return b[0]>a[0]; | |
}); | |
//grab the 3rd greatest | |
return matrix[2][1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function ArrayChallenge(strArr) {
var matrix=[];
matrix = matrix.sort()
matrix = matrix.slice(1, matrix.length-1)
return matrix[0][1]
}
// keep this function call here
console.log(ArrayChallenge(readline()));
``