# Find the longest word in a string question

Find the longest word in a string question
0

#1

When I key in my answer, chrome console gave me an error said:

unexpected token <

I dont know what should I change.

``````function findLongestWordLength(str) {
var words = str.split(' ');
var maxLength = 0;

for(var i = 0 , i < words.length , i++){
if(words[i].length>maxLength){
maxLength=words[i].length;
}
}

return maxLength;

}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
``````

#2

You should review the syntax for a for loop. You are using incorrect characters to separate the 3 parts of the for loop.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops

#3

Look closely at the punctuation in your for…loop

#4

okay I changed the syntax of for loop.
then why does this return 4 instead of 5??

``````function a(str){
var words = str.split(' ');
var maxLength = 0;

for(var i = 0;i < words.length;i++){
if(words[i].length>maxLength){
maxLength = words[i].length;
}
return maxLength;
}
}

console.log(a("This is just a simple test"));
``````

#5

Think carefully about when you should return maxLength.

Hint: How many iterations does your for loop make for the example call you wrote? Use a console.log statement to find out.

Also, the function should return 6 and not 5, because “simple” has 6 letters in it.

#6

I think I fixed it.

Thanks for telling me how to do instead of what to do!!!
Added in a console.log and make it show me what data it output then I will know when to tell it to stop.
Cos I think that after return statement, everything stops, and I simply made the program stopped too early.

``````function a(str){
var words = str.split(' ');
var maxLength = 0;

for(var i = 0;i < words.length;i++){
if(words[i].length>maxLength){
maxLength = words[i].length;
console.log("new length value is: " + maxLength);
}
}

return maxLength;

}
console.log(a("Thisasdasdasdasalsgfjsudybf cybgsfgy is just a simpleaaaaaaaaa test"));
``````