Longest word in a sentence

Longest word in a sentence
0

#1

Anyone here can help me regarding my code and tell me what is wrong in this code ??
*
*

function findLongestWord(str) {
  string = "";
  s = "";
  a = 0;
  for(var i = 0; i < str.length; i++){
    string = string + str.charAt(i);
    if(str.charAt(i) == " "){
      s = string.length;
      if(s>a){
        a = s;
      }
      
    }
  }
  
  
  return a;
}

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

#2

The following line is causing string to grow by one character at each iteration of the for loop.

string = string + str.charAt(i);

Then when you encounter a " ", you set s to string’s length and then set a to s if s is greater than a. This is all fine, but you need to set string back to an empty string within this if statement’s code block or string will just keep growing.


#3

So, after if statements i should add, string = “” , then will it work??
btw thaks for the answer//
or what should it edit??


#4

You want to set string = “” within the if statement block. I will let you figure out exactly where, because this is a great learning opportunity.


#5

Thank you so much…yeah really amazing people here!
But its still not working, i have set string = “” after a = s;
why?
it is returning 12 instead of 6


#6

Can you post your code again? I need to see exactly where you put it.


#7

function findLongestWord(str) {
string = “”;
s = “”;
a = 0;
for(var i = 0; i < str.length; i++){
string = string + str.charAt(i);
if(str.charAt(i) == " "){
s = string.length;
if(s>a){
a = s;
string = “”;
}

}

}

return a;
}

findLongestWord(“May the force be with you”);


#8

When I said inside the if statement, I was referring to the

if(str.charAt(i) == " "){

I will give you a hint by asking a question. Do you only want to reset string to blank if string’s length (s) is greater than a (the longest length so far) or after any string length?


#9

its just that every time we find space, we store the length of string before space.And then empty the string and assign it upto the next space we get , then compare its length to the previous string’s length…
Btw now it will work??
And also we resret string after every space…


#10

I think I may not have phrased my question correctly. I was trying to get you to see that you should not put the string = “” inside the if (s>a) block. You simply need to put the string = “” on the line after this if statement block. I will put the answer below (blurred) in case you still want to try to fix it yourself.

      if(s>a){
        a = s;
      }
      string = ""; // since we need to start building a new word regardless if s > a

#11

Thank you so much …
Btw sir what is your job:?
Beside helping students like me…lol


#12

I have my own company which provides web services for real estate companies.


#13

Thats great sir… Any advice for me to get a dev. job faster?
In which topics i should practice most?


#14

I suggest reading through the many topics in the Getting a Developer Job part of this forum. You will see many have asked this exact question and have received many great responses.