So I'm trying to find the longest string in an array of strings. I've done a problem similar to this before where I had to return the length of the longest string. The thing is, my code works and returns 11 when it looks like this:
var long1= 0;
var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"];
function longestString(arr){
for (i=0; i<arr.length; i++){
if (arr[i].length > long1){
long1= arr[i].length;
}
}
return long1;
}
but, if I change long1= arr[i].length; to long1 = arr[i]; it just returns arr[0] instead. Am I missing something here? The loop seems to be iterating correctly otherwise.
Edit: that is to say, it returns bbllkw.
long1= arr[i].lengthandreturn arr[long1]at the end.long1contains longest length not index. Soarr[long1]will very likely be undefined.