Created
October 6, 2013 16:25
-
-
Save christopherhill/6856009 to your computer and use it in GitHub Desktop.
Find the longest string in a given array.
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
function longestString(i) { | |
// i will be an array. | |
// return the longest string in the array | |
var longestIndex = -1; | |
for (var j = 0; j < i.length; j++) { | |
var curStr = i[j]; | |
var maxStr = longestIndex > -1 ? i[longestIndex] : ''; | |
if (typeof(curStr) === 'string') { | |
if (curStr.length > maxStr.length) { | |
longestIndex = j; | |
} | |
} | |
} | |
return i[longestIndex]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment