Find the Longest Word in a String - length problem

Tell us what’s happening:
hello everybody!
for some reason, in the next code, the variable “s” isn’t getting the number from the length of the array strArr.
the error is: "Cannot read property ‘length’ of undefined"
can anyone point the problem please?
i can’t figure it out for about 5 hours now :frowning:

thank you very much!

Your code so far

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



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

Your browser information:

Link to the challenge:

I can see 3 issues and since this is a challenge I will try to give you some guidance so you can try and figure it out :slight_smile:

First, check your for loop condition, is it within the array’s boundaries. going out of bounds will give you undefined

The Second is the what is j meant to be a String or an int?

The third is the str doesn’t seem to be in the right place. Also, think about when do you want to get or change the word?

Hope this can give you a little direction if you still confused just ask. Good luck :smiley:

first of all. thank you very much for the answer.
I’m looking like you said, for the problems to know how to deal with them, I’m sure I can find the answer online, this was not my intention…:slight_smile:
let me ask you another question, in this function:

function findLongestWord(str) {
 var strArr = str.split(" ");
 console.log(strArr.length);
 var sss;
 var j = 0;
 var i = 0;
 var s = strArr.length;
 console.log(s);
 for(i=0;i>s;i++){
   if(strArr[i].length > j){
     sss = strArr[i];
     j = strArr[i].length;
   }
  }
 str = sss;
console.log(str.length);
}
findLongestWord("The quick brown fox jumped over the lazy dog");

the string in sss is not defined because it is inside the if?

Sorry for the misunderstanding, some people dislike giving them the answer and just want the issues to be pointed to guide them at first then have a discussion if necessary. :slight_smile:

The reason for it being undefined is because of the for loop condition. It should be i<s yep that little mix up creates unimaginable problems :expressionless:
So what was happening was that because what you had was giving you a false loop condition so the loop was being skipped entirely.

When a for loop executes the condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates. If the condition expression is omitted entirely, the condition is assumed to be true. More here about loops

Hope it helps :smiley:

1 Like

this was very helpful, this means that as long as the condition written in the for loop continue to exist, the loop continues to engage.

thank you very much, my friend, this means that this was my only mistake, just to clarify for myself if I define a var outside the loop it exists at all levels?

Well yes, the condition is what keeps the loop going having it empty or just true will make it forever true and leading it to become an infinite loop which is bad*

When I started programming, I disliked for loops because they were like word/method from a foreign language to me unlike if statements and while loops which both can follow/align with my set of instructions I am intending to code. They introduced new learner to it
Something similar to this was what introduced me to understand for loops, helped me visualize it.

int i = 0;//initilazation
while(i<length){//condition
//do something

i++;//incrementation
}
//same as 
for(int i = 0; i<length;i++){
//do something
}
//for loop is better in this case because of the in while loop you might miss the i++ or delete it by mistake.

Well yes within a function, if you declare it (using the var) then you can use it throughout the function itself because it will be local or confined to the inside of the function just like if it were declared in an if statement then it will be confined to the inside of the if statement. this might help better explain it :smile:

console.log("Thank you!")