简体   繁体   中英

Find the Longest Word in a String javascript

My code was work well with string like "The quick brown fox jumped over the lazy dog".

But not work with string like "Google do a barrel roll".

It says problem is "TypeError undefined is not an object(evaluating 'Astr[i].length') ".

 function findLongestWord(str) { var Astr = str.split(" "); var t = Astr[0].length; var Al = Astr.length; var j = 0; for(var i =1; i < t;i++) { if(t < Astr[i].length) { t = Astr[i].length; j = i; } } str = Astr[j]; return str.length; } findLongestWord("Google do a barrel roll");

you have problem with the variables in your 'for' loop.

As you can see, you split the array and get the length of the first member in the array So basicly you get the first word length instead of the word count

var Astr = str.split(" ");
var t = Astr[0].length;

Here you can see that you use 't' (the first word length) as your loop bounds.

for(var i =1; i < t;i++)

Keep your code simple & readable this way it will be maintainable.

function findLongestWord(str) {
   var words = str.split(" ");
   var words_count = words.length;

   var longest_word_length = 0;

    for(var i = 0; i < words_count; i++){
       if(longest_word_length < words[i].length){
           longest_word_length = words[i].length;
       }
    }
    return longest_word_length;
}

findLongestWord("Google do a barrel roll");

Note that you always can use short-hand functions for that

function findLongestWord(str) {
  return str.split(' ').reduce(function(longest, cur) {
     return (cur.length > longest.length) ? cur : longest;
  }, '').length;
}

findLongestWord("Google do a barrel roll");

Here is one way of improving your function:

var str = 'Google do a barrel roll';

function findLongestWord(str) {
  var Astr = str.split(' ');

  if (!Astr.length) {
    throw new Error('findLongestWord(): no words in str');
  }

  var t = Astr[0].length;
  var Al = Astr.length;
  var j = 0;

  for(var i = 0; i < Al; i++)
  {
      if(t < Astr[i].length)
      {
        t = Astr[i].length;
        j = i;
      }
  }
  str = Astr[j];
  return str.length;
}

findLongestWord(str);
//=> 6

You can also do something like this (which is a little easier to understand):

str.split(' ').reduce(function(longest, cur) {
  return (cur.length > longest.length) ? cur : longest;
}, '');
//=> Google
function findLongestWord(str)
 {var arr=[];
 arr=str.split(' ');
 arr=arr.sort(function(a,b)
      {
 return b.length-a.length;   /*sorting the array in decending order of 
                                        lengths of each word*/
      });
 var st=arr[0];           /* obviously the first element of the array will 
                             have longest length.*/
 return st.length;
    }
findLongestWord("Google do a barrel roll");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM