Solution: Find the Longest Word in a String - freeCodeCamp (Basic Algorithm Scripting)

 Find the Longest Word in a String

Return the length of the longest word in the provided sentence.

Your response should be a number.


SOLUTION:


function findLongestWordLength(str) {
  let arr = str.split(" ");
  // console.log(arr)
  let result = arr[0];
  for(let i=0i<arr.length;i++) {
    if(result.length < arr[i].length) {
      result = arr[i]
    }
  }
  return result.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");
console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"))

longest word in a string

Click here to go to the original link of the question.


Post a Comment

Previous Post Next Post