FInd the Longest Word algorithm

I’ve tried the [Read-Search-Ask] method. I just can’t figure out why this code isn’t passing the tests. I usually default to " I definitely missed something," but I really think that this code should work.

  **Your code so far**

function findLongestWordLength(str) {
//loop through string and count letters
let brokenString = str.split(' ');
let longestWord = 0;

for(let i = 0; i < brokenString.length; i ++ ){
 if(brokenString[i].length > longestWord){
   longestWord = brokenString[i].length;

return longestWord.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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36

Challenge: Find the Longest Word in a String

Link to the challenge:

Hello and welcome!

The problem here is that you’re returning longestWord.length. You are already setting longestWord to the value you want.

Hope this helps!

Thank you so much eygis! I understand!

1 Like

This is already a length

So it doesn’t make sense to take a length here. Numbers don’t have the length property.

I would like to contribute with my decision, hope you would like it :no_mouth:

First we split the the string,
then secondly sort it from the longest word to the smallest,
probably the longest word will be in position 0([0]) and then find it’s length.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

Alright, I’m sorry I can move ahead and re-edit or remove it.

1 Like

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