Find the Longest Word in a String lesson

Hey guys, so this is what I managed to do on my own, I can’t seem to find a way how I count the length of the words… any advice for me?

  **Your code so far**

function findLongestWordLength(str) {
const sentence = str.split()
const word = str.split(" ")
for (let i = 0; i < word.length; i++) { 


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/97.0.4692.71 Safari/537.36

Challenge: Find the Longest Word in a String

Link to the challenge:

You can find the length of a string using the length property:

"ABCDE".length === 5

I think what you really need to do is explain in plain English how you are going to do this. If I gave you the following sentence:

“What is the longest word in this sentence”

What would you do to find the longest word?

1 Like

if you want me to give you an answer using javascript I honestly dont know…
I can do .length on every word.

But I didn’t ask you to explain it in javascript, I asked you to explain it in English. Looking at that sentence, what would you need to do to determine the longest word? You can’t just say “I would look at it and see that ‘sentence’ is the longest word” because that’s not really what is going on in your brain. What method would you use to make sure you find the longest word?

in my brain I would see which word has the most characters and count them if needed.

So you would just randomly look at words in the sentence and hope you found the longest one?

I mean if we are not talking about JavaScript then yes I would just look and see… with my eyes

And if I gave you the following sentence:

“lkajflasjflakjflakjdf lajsdflaksjflajsflkjdsflksadjf ljasdflakjsflasjflajsdlfsajlfdjlas ljasdlfsdkajflsajsfaslfjlkajsdlkadsj lajsdlfsdajkflajsfljalfsjasljfasldfj lakjsdflkajsdfladsjsldfjsadljflsad lajsdflasjdflkadsjflajlajflasj lkjasdlfjasdflsadjlfdjaslkfj lkasdjfalksjflsdajfljaslfsadjl lkadsjfldasjflkjaslksajdfl”

You are going to be able to just look at it and tell which one is the longest?

nope, I would have to count every word and compare. the characters i mean

Now we are getting somewhere :slight_smile: So you need to begin to translate “count every word and compare” into JS. You started out with

const word = str.split(" ")

Why did you do this?

the spread the words into arrays, basically to spread the words.

otherwise it was a big sentence or just characters.

Perfect. Since it’s an array of words I would recommend changing the variable name to words but that’s a minor nitpick.

Next you did the following:

for (let i = 0; i < word.length; i++) { 


Why did you do this?

with this i can count the words, word 1 word 2 etc.

What are you trying to count about each word?

I wanted to compare something lol

And what is that “something” you want to compare? What is the ultimate goal of this function?

I need to wrap this up. I think you need to make a good faith effort at coding this and if you can’t get it to work you can paste your code back in here. But you can’t just have an empty for loop and expect us to tell you what to put in there.

Okay I’ll try to compare the characters between the words I guess thats what I need to do.
but just thinking about this my brain does a lot of stuff automatically but translating it into programming is a different story.

That is what programming is. Translating stuff from your brain into code. But you have to know how to do it in your brain first before you can translate.

And you may think your brain is doing stuff automatically, but if you really think about it, you are going through steps to get the answer just like a computer would.


So, you have a loop that iterates over each word in the sentence. First off, just try making a log of each word’s length;

const words = str.split(" ")
for (let i = 0; i < words.length; i++) {
  console.log(/* TODO: length of this word */);

ok so for example now I have list of words length, 4,5,6,3,4 for example.
Now, How can I say that 6 is bigger than the rest? so i need to say if 6 is bigger than something to print it, but how do i tell the computer that 6 is the biggest…