Longest word in String

Hi fellow campers, bit confused on this exercise. Feel like am spinning my wheels yet again. But this is my code so far:

function findLongestWordLength(str) {
str = str.replace(/[^0-9a-z' ']/gi, ' '); // take out punctuation
var words = str.split(' '); // separate words in the string and stores in array
console.log(words); // print items from array
for (var i = 0; i < words.length; i++); {
findLongestWordLength('May the force be with you.');

The line that is stumping me is in the for loop because the output is saying it’s undefined so had a look at the console in my browser and am not really sure what to look for. Just saw one ReferenceError and it included a piece of code ```if®throw Error("Invalid Chai property: "+e+’.length. Due to a compatibility issue, “length” cannot directly follow “’+e+’”. Use “’+e+’.lengthOf” instead.’);’’’

When lengthOf```` was used in place oflength``` in the qualifier on the for loop it returned the first element of the array only.

Then tested explicitly stating the index location in the string that’s being passed (keeping the loop’s qualifier as words.length) and it returns the correct word that is expected. I haven’t put in the piece to check the length yet but already have the idea how to do that.

Any advice?

1 Like


You have extra semicolon when using the for loop.

The code should be like:

for ( ... ) {
 // your code

However, your current solution is like:

for ( ... ); { // <-- you do not need that semicolon
 // your code

I hope this will help. :slight_smile:


@timagixe thank you!