I wrote such code and i can’t understand why my loop doesn’t work (it shows check mark only for example “Google do a barrel roll” where the the first word is longest. i would be glad any advice before i will go to see someone’s code.
Your code so far
function findLongestWord(str) {
var arr = str.split(' ');
for (var n = 0;n <=arr.length; n++) {
var max = 0;
if (arr[n].length >= arr[n+1].length) {
if (arr[n].length > max)
max = arr[n].length;
}
else {
if (arr[n+1].length > max)
max = arr[n+1].length;
}
return max;
}
}
findLongestWord("Google do a barrel roll");