Can anyone the help with the solving approach?

  **Your code so far**

function findLongestWordLength(str) {
return str.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

Challenge: Find the Longest Word in a String

It doesn’t look like you have tried anything yet. What part of the instructions do you need help understanding?

I am not able to figure out what technique to apply

How would you do this task with pencil and paper?

Can you understand it in pseudo code? Or if you had to explain to someone how to do this, step by step, assuming they were a child, how would you explain a fool proof process?

Like if the task were to find the lowest number in an array, I might explain it as:

  1. Start at the beginning of the array and assume that the first number/ element is the lowest number.
  2. Check the next number - if it is lower than the current “lowest number”, replace the current “lowest number” with that current element.
  3. Go to the next number and repeat step 2, keep repeating until the list is done.
  4. When you’ve finished the list, return your current “lowest number”.

Does that makes sense? Being able to break down a problem like that is very good for doing algorithms. Most of us if asked to do that task in the “real world” would just quickly scan the list and “know” what the answer is. But computers are dumb and are terrible at “intuitive” things, so you have to break it down to very simple instructions.

Can you do that for the problem you are facing?

3 Likes

Consider your input.

What is the input you have? It is a string variable.

What will you need to do with the string?

From the sample code, you should know that for a string, you have access to it’s length using varname.length, but that is not helpful if the length is for the entire input.

So, the 1st problem is that you need to find a way to take the original string (input/argument) and extract from it all the words inside the string. One convenient method would be to find a way to take the argument “The quick brown fox jumped over the lazy dog” and convert that into an array that contains the words in the sentence.

Is there a way to do that?

With an array of words, could you then find the length of each element, and keep track of the largest length?

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