js find longest word in string function
snippet in javascript

javascript find the longest word in a string

user7762

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

// 6

js find longest word in string function

user4321

function findLongestWordLength(str) {
  return Math.max(...str.split(' ').map(word => word.length));
}