[JS]返回最长字符串的长度

题目描述:

找出最长单词

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

函数的返回值应该是一个数字

题目来源:freecodecamp

全部代码:

 1 function findLongestWord(str) {
 2   // 请把你的代码写在这里
 3   var arr=str.split(" ");
 4   var s=arr.reduce(function(pre,cur){
 5     if(pre.length>=cur.length) return pre;
 6     else return cur;
 7   });
 8   return s.length;
 9 }
10 
11 findLongestWord("The quick brown fox jumped over the lazy dog");

@jm_epiphany

原创供学习参考使用,转载请注明出处http://www.cnblogs.com/cuphoria/ @jm_epiphany
原文地址:https://www.cnblogs.com/cuphoria/p/9591916.html