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

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

 /*
先把字符串 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. RabbitMQ(二)

    一.启用 rabbitmq_management 插件(官网提供的 web 版管理工具) cd /usr/sbin rabbitmq-plugins enable rabbitmq_managemen ...

  2. 【knowledgebase】如何知道partition数

    对于调优和排错来说,查看一个RDD有多少个partition是非常有用的.常用的查看方法有如下几种: 1.通过SparkUI查看Task执行的partition数 当一个stage执行时,能通过Spa ...

  3. oracle工作经验(左右连接、decode)

    oracle左右连接:select a.studentno, a.studentname, b.classname from students a, classes b where a.classid ...

  4. hive 全局排序

    不分发数据,使用单个reducer ; select * from dw.dw_app where dt>='2016-09-01' and dt <='2016-09-18' order ...

  5. 【SSM 2】spring常用注解

    声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...

  6. thinkphp实现单图片上传

    $config=array( 'maxSize' => 3145728, 'savePath' => './Public/Uploads/', 'rootPath' => './', ...

  7. UITaleView的基础使用及数据展示操作

    UITableView表视图,是实用的数据展示的基础控件,是继承于UIScrollView,所以也可以滚动.但不同于UIScrollView,UITableView只可以上下滚动,而不能左右滚动. 因 ...

  8. Windows7 IE10运行不了JavaScript的问题

    如题,我的环境是Windows7 + IE10,JavaScript怎么也运行不了.郁闷了好一段时间. 后来发现一种办法终于可以让JavaScript运行起来. 具体:  点击 [工具] => ...

  9. SVD++:推荐系统的基于矩阵分解的协同过滤算法的提高

    1.背景知识 在讲SVD++之前,我还是想先回到基于物品相似的协同过滤算法.这个算法基本思想是找出一个用户有过正反馈的物品的相似的物品来给其作为推荐.其公式为:

  10. 跨域文件 clientaccesspolicy.xml

    <?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-do ...