[Solved] Find the Longest Word in a String challenge problem

Tell us what’s happening:

I’m trying to do like the challenge says. By checking if the first word length and the second word length, then doing some stuff, to be in the end, 1 array with 1 word that has the biggest length.

My problem is when I do str.shift(); in else if statement, it says (str[i] is undefined);
)
I don’t understand what’s wrong. Also, when I remove else if, it doesn’t work properly, so please any help?

Your code so far

function findLongestWord(str) {
  str = str.split(" ");
  for (var i = 0; i < str.length;i++){
    if (str[i].length < str[i++].length) {
     str.shift();
    }
    else if(str[i].length == str[i++].length) {
    str.shift();
    }
    else {
      str.splice(1);
    }
  }
  return str[0].length;
}

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

Can you provide the link so that we can test and see whats wrong in your code

https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string

ok i just tested it and the thing is you need to use an additional flag element to save the data.But here you didn’t do that . take a look at my code

 function findLongestWord(str) {
  var words=str.split(" ");
  var flag=0;
  for(var i=0;i<words.length;i++){
    if(words[i].length>flag){
      flag=words[i].length;
    }
  }
  return flag;
}

Here im using a flag to save my number and the algorithm works something like this.
initially i set the flag to 0 then i check every words if it longer than flag. if it is i save it to the flag.and then the process goes on untill we get the longest word.

1 Like

Excuse me but I dont understand your code.

Why you made an additional variable? Also, why you made a condition between the array length of every element and flag?

Oh, I understood now. You made an additional variable to save the length of every element.

Like:
“Chrome is a browser”

1-it checks the length, since Chrome.length is more than flag(0) then you save it, then the loop repeats, i => 1, it checks is, since “is” is not more than 6, then it doesn’t save it so it flag doesn’t change, and keeps like that until it reach “browser” since 7 is more than 6, flag = browser.length, flag = 7.

So the longest length is 7. Wow, that is much better than my long code. I overthink it by including a condition for every situation. Thank you really for helping!

1 Like

ok see initially i set the flag to 0 .correct ?!
var flag=0;
Then i loop through each words in the words array. where all the words were separate.

 for(var i=0;i<words.length;i++)

Then i set an condition If words[i] means the first word’s length in words array is greater than flag which is equal to 0.

if(words[i].length>flag)

if it is than reset theflag variable to the length of the first word that it was compared. which is words[i].length cool ?!
so i did that i reset the flag to the first word’s length like this :

if(words[i].length>flag){
      flag=words[i].length;

So guess if the tested string is rock tim . so by our logic till now the first word is “rock” is greater than flag(0) so we reset the length of rocktim to flag . so now flag is 4 .
The thefor loop again goes and do the samething , but this time it tested the second word if its greater than flag and as we know flag is now 4. so the secod one which is tim is definately not longer than flag. so then we jus print out the flag. and we get the longest value word.

1 Like

Thanks for the accurate explaination! It was really useful.

1 Like