找出最长单词

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

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

当你完成不了挑战的时候,记得开大招'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. java中mysql查询报错java.sql.SQLException: Before start of result set

    异常:java.sql.SQLException: Before start of result set 解决方法:使用rs.getString();前一定要加上rs.next(); sm = con ...

  3. EasyUI的datagrid加载数据去掉遮罩

    转自:https://blog.csdn.net/why15732625998/article/details/77977570 代码: $(".datagrid-mask").r ...

  4. 泛型List去除重复指定字段

    泛型List去除重复指定字段ID var list=listTemp.Distinct(new IDComparer ()).ToList(); 重写比较的方法: public class IDCom ...

  5. centos7下编译安装php7.3

    一.下载php7.3的源码 https://www.php.net/downloads.php 下载php-7.3.4.tar.gz 二.安装gcc,gcc-c++,kernel-devel yum ...

  6. linux 7 更改主机名

    1.在/etc/default/grub 中的GRUB_CMDLINE_LINUX 加上两条参数 #vim  /etc/default/grub GRUB_CMDLINE_LINUX="cr ...

  7. vue-cli 2.x脚手架build目录中的webpack.base.conf.js配置文件

    此文章用来解释vue-cli脚手架build目录中的webpack.base.conf.js配置文件,适用于vue-cli 2.x版本 此配置文件是vue开发环境的wepack相关配置文件,主要用来处 ...

  8. docker安装nginx和php

    参考文章:https://www.cnblogs.com/boundless-sky/p/7182410.html 1.下载镜像docker pull nginxdocker pull php:7.2 ...

  9. mr统计每年中每月温度的前三名

    weatherMapper package com.laoxiao.mr.weather; import java.text.ParseException; import java.text.Simp ...

  10. left join中where与on的区别

    举例进行说明,我们现在有两个表,即商品表(products)与sales_detail(销售记录表).我们主要是通过这两个表来对MySQL关联left join 条件on与where 条件的不同之处进 ...