Javascript String find longest word

Description

Javascript String find longest word

function findLongestWord(str) {
  var arrSentence = Array(str.split(" "));
  var longestLengthSoFar = 0;

  for (var i = 0; i < arrSentence[0].length; i++) {
    if (arrSentence[0][i].length > longestLengthSoFar) {
      longestLengthSoFar = arrSentence[0][i].length;
    }  //from   www .  ja va2s  . co m
  }
  return longestLengthSoFar;
}
console.log(findLongestWord("The quick brown fox jumped over the lazy dog"));
console.log(findLongestWord("May the force be with you"));
console.log(findLongestWord("Google do a barrel roll"));
console.log(findLongestWord("What is the average airspeed velocity of an unladen swallow"));
console.log(findLongestWord("What if we try a super-long word such as otorhinolaryngology"));



PreviousNext

Related