Find the longest word in a string question

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");

Look closely at the punctuation in your for…loop

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"));

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"));
1 Like