freeCodeCamp:Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度。
函数的返回值应该是一个数字。
/*
先把字符串 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的更多相关文章
- #254 Find the Longest Word in a String
找出最长单词 在句子中找出最长的单词,并返回它的长度. 函数的返回值应该是一个数字. 当你完成不了挑战的时候,记得开大招'Read-Search-Ask'. 这是一些对你有帮助的资源: String. ...
- Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. 这是一些对你有帮助的资源: String.split() String.length 第一种想法就是,先定一个小变量,来他一 ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- Find the Longest Word in a String-freecodecamp算法题目
Find the Longest Word in a String(找出最长单词) 要求 在句子中找出最长的单词,并返回它的长度 函数的返回值应该是一个数字. 思路 用.split(' ')将句子分隔 ...
- [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. ...
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- [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 ...
- [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 ...
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
随机推荐
- 在Linux中永久设置Anaconda环境变量的方法
[感谢:http://www.codesec.net/view/459539.html] 如果在安装Anaconda的过程中没有将安装路径添加到系统环境变量中,需要在安装后手工添加: 1.在终端输入$ ...
- [Tex学习]WinEdit 常用软件快捷键
WinEdit 常用软件快捷键 编辑: Alt+C:在剪贴板原有复制文本后增加新的被选择的文本. Ctrl+Shift+Alt+Right/Left:对选中文本增加或者删除Comment标记. Ctr ...
- ssh 协议执行repo sync 报错:Permission denied (publickey)
1.ssh key 已经添加ssh key到gerrit服务器,并且执行ssh协议的git clone可以正常克隆代码到本地,可见不是ssh key的问题. 2.manifest清单文件配置 最初在m ...
- 用ping命令来模拟traceroute的功能
ping -n 1 -r 9 qq.com 正在 Ping qq.com [163.177.65.160] 具有 32 字节的数据:来自 163.177.65.160 的回复: 字节=32 时间=11 ...
- R&S学习笔记(一)
1.一个VRF有两个主要的组成部分:路由区分符RD和路由目标RT.一个路由区分符(RD)是一个数字,除了帮助识别在一个提供商的网络中的VPN和允许重叠 IP区域之外没有其它的含义.RD是一个分为两个部 ...
- Karma 5:集成 Karma 和 Angular2
集成 Karma 和 Angular2 我们需要做很多工作,由于需要使用 TypeScript 进行开发,首先需要正确配置 Typescript ,然后正确配置对 Angular2 的引用.还要创建 ...
- Node.js初探之hello world
昨天公司内部培训,主讲人王老板对Node.js评价很高,连用几个“变态”来形容,恰好今天周末,有时间来认识下Node.js,对一门新语言最好的认识,是让其输出“hello world”,今天我就利用N ...
- 洛谷P3366 【模板】最小生成树
P3366 [模板]最小生成树 319通过 791提交 题目提供者HansBug 标签 难度普及- 提交 讨论 题解 最新讨论 里面没有要输出orz的测试点 如果你用Prim写了半天都是W- 题目 ...
- C++学习基础十——子类构造函数与析构函数的执行
1.子类构造函数的执行: 先执行父类的构造函数,再执行成员对象的构造函数,最后执行自身的构造函数. 当继承多个类时,构造函数的 执行顺序与继承时的顺序 相同,而与子类构造函数调用父类构造函数的顺序无关 ...
- 使用 Intellij Idea 导出JavaDoc
使用/* ...... /来注释代码,解释方法参数,返回参数,类的功能及用法. 常用的注释标签: @author 作者 @version 版本 @see 参考转向 @param 参数说明 @retur ...