Tell us what’s happening:
My plan is to:
- Convert the string to an array
- Count the characters in each word
- store the values to compare
- compare the word lengths
- return the longest word in the string
I realize this may not be the most logical or quickest way, but this is the process that came to mind.
I am really having a hard time understanding for and for/of loops. I feel like I need to do that in some way shape form and fashion here. And I cannot for the life of me figure out where my missing semicolon is…
**Your code so far**
function findLongestWordLength(str) {
let words = str.split(' ');
String longestWord = str[0];
for(int i = 1; i < str.length; i++) {
String word = str[i];
if(word.length() > longestWord.length())
longestWord = word;
}
return word.length;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
**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.182 Safari/537.36
.
Challenge: Find the Longest Word in a String
Link to the challenge: