[removed] find longest word in a string

前端 未结 30 2714
余生分开走
余生分开走 2020-11-27 03:50
function longestWord(string) {
    var str = string.split(\" \");
    var longest = 0;
    var word = null;
    for (var i = 0; i < str.length - 1; i++) {
                


        
30条回答
  •  鱼传尺愫
    2020-11-27 04:17

    I find that the .map method here helps a lot (this is if you want the character count of the word, not the word itself):

     function findLongestWord(str) {   
       var array = str.split(/\s+/);
       var wordLength = array.map(function(i) {
         return i.length;                       
       });   
       var largest = Math.max.apply(Math, wordLength);   
       return largest; 
    }
    

提交回复
热议问题