So I am doing one of the algorithm challenges. i think I have a solution but I keep getting the error. “cannot read property length of undefined”
This is my code.
function findLongestWord(str) {
arr=[""];
arr=str.split(" ");
var result = 1;
for( i=0 ; i<= arr.length;i++){
if(arr[i].length>= result){
result=arr[i].length;
}
}
return arr;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
I have tried breaking it apart, but to no avail, Can anyone see where I am going wrong ?
Thanks in advance