[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

posted @ 2018-09-05 14:28  jm_epiphany  阅读(511)  评论(0编辑  收藏  举报