Find longest word algorithm

Given a sentence, find the length of the longest word in it.

Returns the length of the longest word in the provided sentence.

The return value should be a number.

Use split() to split each word in the sentence into an array and compare the longest words.

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

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256406&siteId=291194637