Algorithm find longest word in a string1

Tell us what’s happening:
typed the soln its not working

Your code so far

function findLongestWordLength(str) {

var words=str.split('');

var maxLength=0;

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

   if (words[i].length>maxLength) {

     maxLength=words[i].length;

   }

 }

return maxLength;

}

function findLongestWordLength(str) {
var words=str.split('');
var maxLength=0;
for (var i=0; i<words.length;i++) {
  if (words[i].length>maxLength) {
    maxLength=words[i].length;
  }
}
return maxLength;
}

Your browser information:

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

Challenge: Find the Longest Word in a String

Link to the challenge:

add console.log(words) inside your function to see if it’s happening what you want

They is an issue with what you are assigning to your variable.

What is your result? It’s difficult to find the error if we don’t know what happens whit your code. As said @ieahleen, it’s a good idea place some console messages to view what happens whit the variables trought the whole algoritm.
At first sight, i think may be a mistake in your “split” function. You used a empty string as words separator, and must be an space: ’ ', instead ‘’.

Well could u elaborate

@jmnqv123 Give a space inside str.split that is str.split(’ ').

Of course.
When you use a function “split”; what you do is to separate a string in diferente pieces, acording to a specific parameter. For what you are looking for, you need to separate the entire string on words. For that, you must a space (" “) as separator. Then, the string will be cutted every time it find a space (between words), and will push the fragment into an array (your “words” variable).
If you use an empty string (”") instead a space (" "), the “split” function will separate not words, but characters.