Find the Longest Word in a String__Doubt

Find the Longest Word in a String__Doubt
0

#1

Tell us what’s happening:
what is wrong with this code? please help me find a bug.
The code works properly for some sentences except this one.

Your code so far

function findLongestWord(str) {
  var a=str.split(' ');
  var b=a.map(function(str1){return str1.length;});
  var largest=0;
  for(var i=0;i<b.length-1;i++)
    {
      if(b[i]>largest)
        largest=b[i];
    }

  return largest;
}

findLongestWord("What if we try a super-long word such as otorhinolaryngology");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0.

Link to the challenge:


#2

Your for loop condition is incorrect.

for(var i=0;i<b.length-1;i++)

You never look at the last element in the array.


#3

thanks for your help!!