Whats wrong with my code, its not passing test cases

Tell us what’s happening:

Your code so far


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





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

Your browser information:

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

Challenge: Find the Longest Word in a String

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

check your split method carefully.

1 Like

Hello!

The problem is that You’re splitting every letter, not just words. Write a console.log(large) right after var large = str.split(''); and then run the tests to see what’s happening. If You cannot figure it out after looking a the output, then come back :slight_smile:.

1 Like

Just add space in Split like var large = str.split(' ');

2 Likes

give one white space while splitting your string. var large = str.split(" "); It will split the string by white space and then can count the longest word.

1 Like