Find greatest length if letters in a string 1

Find greatest length if letters in a string

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;
}

Here is what the above code is Doing:
1. Split the string into an array of words.
2. Create a variable to store the length of the longest word.
3. Loop through the array of words.
4. If the current word is longer than the longest word, update the longest word.
5. Return the length of the longest word.

Similar Posts