Basic Algorithm Scripting - Find the Longest Word in a String

Tell us what’s happening:
Describe your issue in detail here.

The directions say:
"Return the length of the longest word in the provided sentence.

Your response should be a number."

I recently posted how I am seriously struggling with JS and can’t come up with ideas on my own at all…but I don’t want to keep looking at solutions and it’s been recommended to come here with help. So I guess I just am looking for ideas on first steps of this algorithm.

Here’s what I think I know…

  • The code needs to
    1 ) search the string and count the length of each word
    and then
  1. return the word that has the longest length.

Can you suggest which lesson I could go back to or something I should search to get started?

Your code so far

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Find the Longest Word in a String

Link to the challenge:

For algorithm challenges, I will tell you the steps I used when I was first leraning and what helped me get better at these types of challenges.

No.1:
repeat the problem back to yourself and rephrase it in your own words.
You don’t want to spend a whole bunch of time working through a problem when it turns out you were misunderstanding it in the first place.

I found that if you can rephrase the problem and provide your own examples, then that means you have a good example of what you are being asked to do

No.2:
try to solve the problem as a human first and away from code.

jumping into code to early, without thinking about the problem and how to best solve it is a recipe for disaster.

For this particular problem, how would you solve this as a human if you were asked to walk up to a white board and figure out the length of the longest word in a sentence?

Well you would probably go through each word and keep track of its length and then at the end of the sentence see which word had the longest length and return that length

No.3:
write down the steps you just did to solve it as a human.
then slowly translate that algorithm into code.

You have already received some good advice on how to tackle this with code.

By coming up with an algorithm and thinking through the problem will give you a good place to start.
Then if you get stuck with code, you can share you algorithm with the forum and the code you have tried and we can help you debug from there.

Hope that helps

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