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
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");
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…
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.
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
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
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