If statement problem

Tell us what’s happening:
Describe your issue in detail here.
the if statement in the given code executes only only time even it is in the for loop. i couldn’t figure out what’s the mistake i did. can anyone help me with this problem.(sorry for my english)
thanks in advance.

  **Your code so far**

function findLongestWordLength(str) {
str=str.split(" ");
console.log(str.length)
let longest='';
for(let i=0;i<str.length;i++){
  
  if(str[i].length > longest.length){
    console.log(str[i]);
    longest=str[i].length;
  }
  console.log(str[i]);
}
return longest;
//return str.length.sort((a,b)=>b.length-a.length)[0];
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.79 Safari/537.36

Challenge: Find the Longest Word in a String

Link to the challenge:

I don’t understand exactly what you are asking, but there are a couple strange things here

You declare longest as a string

And here you use it as a string

But here you set it as a number

Also, it’s generally a bad idea to overwrite input variables

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.