码迷,mamicode.com
首页 > 其他好文 > 详细

在句子中找出最长的单词,并返回它的长度。

时间:2017-09-15 18:38:20      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:tput   ges   div   air   logs   应该   -o   split   speed   

function findLongestWord(str) {
 
  var arr = str.split(" ");
  var a = [];
  for (var i in arr) {
    a.push(arr[i].length);
  }
  
  return a.sort(function (a, b) {
    return b-a;
  })[0];
}

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

findLongestWord("The quick brown fox jumped over the lazy dog") 应该返回一个数字

findLongestWord("The quick brown fox jumped over the lazy dog") 应该返回 6

findLongestWord("May the force be with you") 应该返回 5.
findLongestWord("Google do a barrel roll")应该返回 6.
findLongestWord("What is the average airspeed velocity of an unladen swallow")应该返回 8.
findLongestWord("What if we try a super-long word such as otorhinolaryngology") 应该返回 19.

在句子中找出最长的单词,并返回它的长度。

标签:tput   ges   div   air   logs   应该   -o   split   speed   

原文地址:http://www.cnblogs.com/spectrelb/p/7527204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!