What am I missing with this string length?

Hey I’m getting larger results with this code than I should be. What am I missing or doing incorrectly? Tried adding [i] to return splitStr[i].length but doesn’t look quite right. Thanks

** update just realized using return splitStr[i].length insn’t working because it’s not storing a string globally trying to figure this out now

Your code so far


function findLongestWordLength(str) {
  var splitStr = str.split(" "); //splitting the string into words

  for (var i = 0; i < splitStr.length; i++) { //running through the loop to log length of each word
    return splitStr.length; //need to return the largest length of a word in the string, but not sure how to store the largest word
  }
}

console.log(findLongestWordLength("May the force be with you")); 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Find the Longest Word in a String

Link to the challenge:

As soon as your function hits the return statement in the first iteration of the for loop it returns the length of splitStr and the function ends. So your function will always return the length of splitStr and will never have a chance to find the longest word in the string.

What you want to do in that for loop is look at the length of the current word you are examining in the array. Is it the longest word you’ve encountered so far? Then you’ll want to save off its length. Basically, you keep track of the longest word length you’ve come across. After you’ve gone through the entire array you’ll have the longest length.

1 Like

Maybe push those length values store in another array using newArray.push() ?

and then find the the highest value of that array maybe .map() for example

Ok I see where you guys are going with this. I tried writing this out

function findLongestWordLength(str) {
  var splitStr = str.split(" ");
  var longestWord = 0;

  for (var i = 0; i < splitStr.length; i++) {
    if (splitStr[i].length > longestWord) {
       return longestWord.push(splitStr[i]); 
    } else { 
      return longestWord;
    }
  }
}

It’s saying longestWord is not a function… Not sure what to do actually

This is I think the syntax issue you are seeing:

This declares longestWord to be a number.

But here your are trying to use the push method, which is not valid for a number. What about assigning (=) the new length as the longest word length?


Also, I’d move all of those return keywords until below your loop. The very first time your code path hits a return, the loop will stop, so you need to hold off on the return until after your loop finishes.

Thanks. Didn’t realize that. I ended up with this but it’s not yet working:


function findLongestWordLength(str) {
  var splitStr = str.split(" ");
  var longestWord = 0;

  for (var i = 0; i < splitStr.length; i++) {
    if (splitStr[i].length > longestWord) {
       longestWord = splitStr[i].length; 
    } else { 
      longestWord = longestWord;
    }
    return longestWord;
  }
}

You’ve still got the return statement inside of the for loop so the loop is only going to iterate once.

1 Like

You are really close. Your return is below your if but inside your for loop.

2 Likes

@bbsmooth @JeremyLT ah got it! Thank you :pray:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.