[removed] find longest word in a string

前端 未结 30 2535
余生分开走
余生分开走 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:16

    does this solve the problem??

    function longestWord(string) {
        var str = string.split(" ");
        var longest = 0;
        var word = null;
        for (var i = 0; i <= str.length - 1; i++) {
            if (longest < str[i].length) {
                longest = str[i].length;
                word = str[i];
            }
        }
        return word;
    }
    
    document.write(longestWord("pride and prejudice"));
    
    0 讨论(0)
  • 2020-11-27 04:17

    ForEach is faster in FF but slower in Chrome, but for loop with the cached length and function apply/call is quite faster in both FF and chrome.

    Hope the below code helps:

    function getLongest (arrStr) {
      var longest = 0, word;
    
      for(var i=0 , len = arrStr.length ; i < len ; i++){
    
        if(longest < arrStr[i].length) {
           longest = arrStr[i].length;
           word = arrStr[i];
        }
    
      }
    
      return word;
    }
    
    function isLongest (str) {
      var arrayStr = str.split(' ');
      return function(fn) {
        return fn.apply(this,[arrayStr]);
      }
    }
    
    isLongest("hello aaaaaaaaaaaaaaaaaaaaaaaaa bbb")(getLongest); //aaaaaaaaaaaaaaaaaaaaaaaaa
    
    0 讨论(0)
  • 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; 
    }
    
    0 讨论(0)
  • 2020-11-27 04:18

    I will refer you to this awesome article which defines three ways:

    1 - Find the Longest Word With a FOR Loop

        function findLongestWord(str) {
      var strSplit = str.split(' ');
      var longestWord = 0;
      for(var i = 0; i < strSplit.length; i++){
        if(strSplit[i].length > longestWord){
        longestWord = strSplit[i].length;
         }
      }
      return longestWord;
    }
    findLongestWord("The quick brown fox jumped over the lazy dog");
    

    2 - Find the Longest Word With the sort() Method

    function findLongestWord(str) {
      var longestWord = str.split(' ').sort(function(a, b) { return b.length - a.length; });
      return longestWord[0].length;
    }
    findLongestWord("The quick brown fox jumped over the lazy dog");
    

    3 - Find the Longest Word With the reduce() Method

    function findLongestWord(str) {
      var longestWord = str.split(' ').reduce(function(longest, currentWord) {
        return currentWord.length > longest.length ? currentWord : longest;
      }, "");
      return longestWord.length;
    }
    findLongestWord("The quick brown fox jumped over the lazy dog");
    

    Of course, it returns the length of the biggest word if you want to get the string, just get rid of the length in return part.

    0 讨论(0)
  • 2020-11-27 04:18

    function sortNumber(a, b) {
    return a - b;
    }
    
    function findLongestWordLength(str) {
    var split = str.split(" ");
    var arr=[];
    for(var i=0;i<split.length;i++){
      arr.push(split[i].length);
      }
      arr.sort(sortNumber);
      console.log(arr[arr.length-1]);
      return(arr[arr.length-1]);
    }
    
    findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");

    0 讨论(0)
  • 2020-11-27 04:18

    const longestWord = string =>
    {
        stringArray = string.split(' ').sort(
            (a,b) => b.length - a.length)
        let longestArray= stringArray.filter( word => word.length === stringArray[0].length ) 
        if(longestArray.length === 1){
            console.log(longestArray[0])
        }
        else
        {
            console.log(longestArray)
        }
    }
    longestWord("Pride and Prejudice")

    0 讨论(0)
提交回复
热议问题