Find the longest word in a string using javascript

后端 未结 10 1244
长情又很酷
长情又很酷 2021-01-16 23:06

I\'m trying to find the longest word in a string, but it continually returns the length of the first word. Any ideas?

Here\'s my code:

function findL         


        
10条回答
  •  不要未来只要你来
    2021-01-16 23:31

    here is how I did it.

    function findLongestWord(str) {
      var strArray =[];
      var numArray=[];
      var sortedNumArray=[];
    
      strArray = str.split(" ");
    
      numArray = strArray.map(function(val){return val.length;});
    
      sortedNumArray=numArray.sort(function(a,b){return a-b;});
    
      return sortedNumArray.pop();
    
    }
    

提交回复
热议问题