查找字符串中最长的单词

题目

返回给出的句子中最长的单词的长度。

你的返回应该是一个数字。

要求

  1. findLongestWordLength("The quick brown fox jumped over the lazy dog")应该返回一个数字。
  2. findLongestWordLength("The quick brown fox jumped over the lazy dog")应该返回 6。
  3. findLongestWordLength("May the force be with you")应该返回 5。
  4. findLongestWordLength("Google do a barrel roll")应该返回 6。
  5. findLongestWordLength("What is the average airspeed velocity of an unladen swallow")应该返回 8。
  6. findLongestWordLength("What if we try a super-long word such as otorhinolaryngology")应该返回 19。

代码

function findLongestWordLength(str) {
  let strArr = str.split(' ')
  let temp = strArr[0]
  for(let i = 1;i < strArr.length;i++) {
    temp.length < strArr[i].length ? temp = strArr[i] :''
  }
  return temp.length;
}

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

猜你喜欢

转载自blog.csdn.net/kyr1e/article/details/82948053