Basic Algorithm Scripting - Find the longest word in a string

Tell us what’s happening:
Hey guys, so I more or less got the outline of my algorithm. I’m just unsure what I could implement in the loop to loop through the values of the array and get the longest string’s length. Do I have to add another loop or an if maybe? I’m just confused atm. Any advice would be helpful!

Thanks in advance for the help!

Regards
Ma3_str0

``````
function findLongestWordLength(str) {
let strArr = str.split();
for(let i = 0;i < strArr.length;i++){
let loopLength = strArr[i].length;
}
}

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

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0`.

Challenge: Find the Longest Word in a String

now you are putting `loopLength` each time at the length of current word

but you need to compare the various word lengths to each other to find out which is the longest

you are missing that logic

Would I compare them in an if statement? Or how would I compare the lengths? Cause that’s what’s mainly confusing me.

To solve such a challenge, you need a bit of time to think manually about the logic.

And if you can, write the pseudocode than translate it in real-language code.

There’s not at all ONE unique way to think good, no. You just need a bit of logic in your thinking. You can still refactor as many times as you want.

Here’s my suggestions for you:

1. split the param using (’ ') as separator (keep it in a var)
2. set a var as the length asked with 0 (zero) as initial value (let call it strL)
3. loop through the split var
4. if the current str length > strL => strL = current str length
5. return strL

This is just one way you can do it.

1 Like

Thanks for the response my man I’ll try those steps. When I program in general when solving problems or building a random app, I get impulsive and I improv everything along the way and tinker later. But I’ve been trying to first plan in pseudo lately and it’s been helping me slowly but surely! Thanks again for the reply!

1 Like

function findLongestWordLengt(str){

let bunch = str.split(" ");

let dogo = “”;

for ( let single of bunch){

``````if(single.length > dogo.length){

dogo = single;

}
``````

}

``````return str = dogo.length;
``````

}

console.log(findLongestWordLengt(“The quick brown fox jumped over the lazy dog”));

your function has a different name than the one used in the tests

1 Like

The function name is FCC is this `findLongestWordLength()`, yours does not have `h` at the end.

1 Like