找出最长单词

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

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

当你完成不了挑战的时候,记得开大招'Read-Search-Ask'。

这是一些对你有帮助的资源:

代码

function findLongestWord(str) {
// 按照空格分割字符串,生成数组
var strArr = str.split(" "); // 初始化 length 为 0
var length = 0; for (var i = 0; i < strArr.length; i++) {
// 遍历过程中,若当前字符串长度比 length 大,就更新 length
if (strArr[i].length > length) {
length = strArr[i].length;
}
// 不需要 else,因为如果比 length 小,继续执行遍历就可以了
} // 循环结束,返回 length 作为结果
return length;
}

解释

  • 要解释的不多,都在注释里。理解这个思路就好,在 for 循环外设置一个变量,用于追踪最大值。找到更大的就更新,最后返回这个变量就行
  • 至于为什么不在 for 循环里设置变量,因为如果这样,执行循环的每一步都会重新初始化这个变量,我们就不能用这个变量来追踪循环过程了

优化

思路提示

  • 可以优化的方案是,采用数组内置方法 reduce 来实现

参考链接

代码

function findLongestWord(str) {
var stringArr = str.split(" "); return stringArr.reduce(function (prev, next) {
// 返回值为参数与当前字符串中较大的数
// 返回值会作为下次计算的 prev 传入
return Math.max(prev, next.length);
}, 0)
}

解释

  • 如果不熟悉 reduce 语法和参数,请先去上面的链接看一下
  • reduce 的第一个参数为回调,第二个参数为初始值。第二个参数可以为空
  • 举个例子,如果我们传入的字符串是 "a bb ccc d" 执行过程如下:

  • 因此,最终结果为 3
  • 关于 reduce,一句话概括,就是:遍历数组,把上一次计算的结果用于下次计算

#254 Find the Longest Word in a String的更多相关文章

  1. freeCodeCamp:Find the Longest Word in a String

    找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. /* 先把字符串 str 转为数组 myarr 将数组myarr中的每个元素长度转换成一个新的数组newarr 将这个数组按 ...

  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. Spring:AOP

    摘要 本文内容为我在网上搜集Spring AOP资料的汇总.摘抄. AOP是一种编程思想,其对不同对象进行了横向的抽象,将不同对象的.和主流程无关的公共逻辑抽象出来以方便维护.AOP的实现基础为AOP ...

  2. 词根:sol = sun(太阳) 词根:sol = alone/single, whole/entire (单独的)

    词根:sol = sun(太阳) sol 这个词根有的书上如刘毅字典,刘洪波英文字根词源精讲等上面说来自拉丁语的sol(=sun),有的书如赢在单词上面说sol 来自拉丁语的solari,但不管哪种说 ...

  3. angular 遍历foreach

    1.angular遍历angular.forEach(groups,function (group) { })

  4. 20-matlab全排列-函数调用

    matlab中global的用法 Matlab 中子函数不传参直接调用主函数global变量方法  在一个m文件里要调用一个函数(自定义的),但是我希望这个函数能利用并修改workspace中的变量( ...

  5. 把Excel作为数据库,读到DataTable中,Excel科学计数法数字转字符串

    需要引用:using System.Data.OleDb; /// <summary> /// 获取Excel数据,包含所有sheet /// </summary> /// & ...

  6. Android设备直接运行java项目?还杀不死?

    思路:拿到dex可执行文件,使用android执行 使用idea创建java类库,写相关逻辑代码 使用idea导出该类库jar包 使用android dx工具 将jar文件转换为dex可执行文件 dx ...

  7. appium定位toast消息的使用

    定位使用xpath后,定位消息文本,然后使用text获取消息文本做断言.toast_loc = ("xpath", ".//*[contains(@text,'切换运营商 ...

  8. docker run命令运行以及参数详解

    命令格式: -a, --attach=[] 登录容器(必须是以docker run -d启动的容器) -w, --workdir="" 指定容器的工作目录 -c, --cpu-sh ...

  9. jmeter用Windows电脑分布式部署

    当然,java环境.jmeter安装我这里就不说了. 使用1个controller(imac电脑),2个agent(Windows7 系统) 一.agent配置(Windows7系统) 1.电脑环境变 ...

  10. oracle primary key & foreign key

    主键:一个表中只有一个主键约束,但是一个主键约束可以由数据表中的多个列组成:primary key alter table TName add constraints pk_name PRIMARY ...