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

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

 /*
先把字符串 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. JAVA_Java常用核心包(概念)

    20150802 Created By BaoXinjian

  2. Python中利用LSTM模型进行时间序列预测分析

    时间序列模型 时间序列预测分析就是利用过去一段时间内某事件时间的特征来预测未来一段时间内该事件的特征.这是一类相对比较复杂的预测建模问题,和回归分析模型的预测不同,时间序列模型是依赖于事件发生的先后顺 ...

  3. Django views 中 View decorators

    decorators(装饰器) 1. require_http_methods 在django.views.decorators.http中,可以用来限制请求的权限. require_http_met ...

  4. js javascript jquery区别和联系

    javascript:简称js ,是用于客户端Web开发的一种脚本语言,常用来给HTML网页添加动态功能. jquery:基于JS语言,封装JS的原生方法功能,提供了简便的函数接口,简化了JS的操作. ...

  5. 双击vbs时,默认cscript运行脚本

    Dim obj_shellset obj_shell = createobject("wscript.shell")host = WScript.FullNameIf LCase( ...

  6. AngularJS图片上传功能的实现

    一.前言 前一段时间做项目时,遇到一个问题就是AngularJS实现图片预览和上传的功能,当时查阅文档(都是英文文档)折腾了很久才弄出来,现将整个流程整理出来,有需要的朋友可以参考一下,如果您有更好的 ...

  7. VBS编辑字段

    '为已经创建好的数据表添加字段'参数:strDBPath 字符串型 数据库路径'参数:strTableName 字符串型 需要创建的数据表的名称'参数:strColumnName 字符串型 需要添加的 ...

  8. VMware 中windows server 之DHCP 搭建与测试

    感悟: 由于打算将windows server 的服务器搭建维护从头重新学习总结一下,遇到搭建dhcp服务的时候,在虚拟机中一直测试不成功,耽误我好几星期了,一点也不夸张,心情和积极性也大大受到打击. ...

  9. ubuntu 开机显示错误:无法应用原保存的显示器配置

    无法应用原保存的显示器配置CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0)CRTC 63:尝试 2560x1600@60Hz 模式输出在 1366x ...

  10. 2016年 Delphi Roadmap

    2016年delphi Roadmap 发布,这也是新公司的第一次发布路线图. 虽然稍微晚点( 原来说是1月份发布路线图),至少比过去积极点.喧嚣多年的靴子终于落地. Linux 的支持终于正式公布. ...