Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created October 6, 2013 16:25
Show Gist options
  • Save christopherhill/6856009 to your computer and use it in GitHub Desktop.
Save christopherhill/6856009 to your computer and use it in GitHub Desktop.
Find the longest string in a given array.
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