Find the Longest Word in a String help me please

Tell us what’s happening:
Describe your issue in detail here.
Can you tell what is the problem with my code. Thank you

  **Your code so far**
function findLongestWordLength(str) {
let words = str.split(" ");
let longestWord = 0;
for (let word of words) {
 if (word.length > longestWord.length) word = longestWord;
}
return longestWord.length;

}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Find the Longest Word in a String

Link to the challenge:


function findLongestWordLength(str) {
let words = str.split(" ");
let longestWord = "";
for (let word of words) {
 if (word.length > longestWord.length) {
    longestWord = word;
 }
}
return longestWord;
}

var x = findLongestWordLength("The quick brown fox jumped over the lazy dog");
console.log(x)

please see the code above,
you need to return the word itself and not it’s length.
hope that helpes.

1 Like

Don’t you want to update longestWord?

1 Like

So I think that if you want to update word, we write this :word = longestWord, and if we want to update longestWord, we write this:longestWord = word. Is that true? Anyway, thank you for your help

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