找到提供的句子中最长的单词,并计算它的长度。

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

 /*
先把字符串 str 转为数组 myarr
将数组myarr中的每个元素长度转换成一个新的数组newarr
将这个数组按由小到大排序
取此数组中最后的数值,也就是最长的字符串
将这个长度值返回
*/ function findLongestWord(str) {
//把字符串 str 转为数组 myarr
var myarr=str.split(" ");
//定义longest方便调用
var longest=0;
//遍历数组myarr,myarr.length=6
for(var i=0;i<myarr.length;i++){
//遍历新数组myarr并将数组中最大的值赋值给longest
if(myarr[i].length>longest){
longest=myarr[i].length;
}
}
return longest;
} findLongestWord("The quick brown fox jumped over the lazy dog");

freeCodeCamp:Find the Longest Word in a String的更多相关文章

  1. #254 Find the Longest Word in a String

    找出最长单词 在句子中找出最长的单词,并返回它的长度. 函数的返回值应该是一个数字. 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: String. ...

  2. Find the Longest Word in a String

    找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. 这是一些对你有帮助的资源: String.split() String.length 第一种想法就是,先定一个小变量,来他一 ...

  3. FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)

    题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...

  4. Find the Longest Word in a String-freecodecamp算法题目

    Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...

  5. [CareerCup] 18.7 Longest Word 最长的单词

    5.7 Given a list of words, write a program to find the longest word made of other words in the list. ...

  6. [LeetCode] Longest Word in Dictionary 字典中的最长单词

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  7. [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  8. [Swift]LeetCode524. 通过删除字母匹配到字典里最长单词 | Longest Word in Dictionary through Deleting

    Given a string and a string dictionary, find the longest string in the dictionary that can be formed ...

  9. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

随机推荐

  1. Installing Erlang

    Installing Erlang Pre-built binaries for most common platforms. Source code releases from the main E ...

  2. Consuming a Web Service in AX 2012

    Consuming a Web Service in AX 2012 在AX2012版本中如果想调用外部的Web Service变得非常容易. 第一步,在VS中创建一个Web Service并发布 第 ...

  3. appframework(jqmobi) 3.0 设置

    $(document).on("panelunload",'#mainPage',function(e){ alert('dddddd'); }); 1.重写 data-load ...

  4. CGI、FastCGI和PHP-FPM浅析

    这段时间对Nginx+PHP-FPM的概念和机制一直不太清晰,趁着同事的分享和看过的几篇博文和资料,重新将思路处理一下. 首先,PHP-FPM(FastCGI Process Manager: Fas ...

  5. WAMP(Windows、Apache、MySQL、php)安装配置过程常见问题

    WAMP(Windows.Apache.MySQL.php)安装配置过程 可以参考该网友的总结(总结的不错,鼓掌!!): http://www.cnblogs.com/pharen/archive/2 ...

  6. MapReudce中常见join的方案

    两表join在业务开发中是经常用到,了解了大数据join的原理,对于开发有很大的好处. 1.reduce side join reduce side join是一种简单的join的方法,具体思想如下: ...

  7. Scrum 项目——1

    广商检索页面 1) N (Need 需求) 这个页面会按一定的规律来集合广商的一些资源,包括微信公众号.教务系统登录处.宿舍报修等,是为了方便我们整个广商的学生和老师来运用.因为现在虽然有很多微信公众 ...

  8. javascript组件化

    http://purplebamboo.github.io/2015/03/16/javascript-component/#%E5%BC%95%E5%85%A5%E4%BA%8B%E4%BB%B6% ...

  9. ado.net access oracle dataset via store procedure

    使用存储过程返回结果集,并绑定到ado.net对象中在sql server里面是非常直观的. 1: create procedure GetAllRecords 2: as 3: select * f ...

  10. mysql事件定时

    DELIMITER $$ MONTH STARTS '2013-01-07 11:20:00' ON COMPLETION PRESERVE ENABLE DO BEGIN CALL ps(); EN ...