Find the Longest Word in a String-freecodecamp算法题目
Find the Longest Word in a String(找出最长单词)
- 要求
- 在句子中找出最长的单词,并返回它的长度
- 函数的返回值应该是一个数字。
- 思路
- 用.split(' ')将句子分隔成各个单词组成的数组
- 定义一个temp变量,将数组第一个元素赋值给它
- 在for循环中用数组剩余元素的长度对比temp,最后将最大长度的元素赋值给temp
- 返回temp长度
- 代码
function findLongestWord(str) {
// 请把你的代码写在这里
str = str.split(' ');
var temp = str[0];
for (var i = 1;i<str.length;i++){
if (temp.length<str[i].length){
temp=str[i];
}
}
return temp.length;
} findLongestWord("The quick brown fox jumped over the lazy dog");
4.相关链接
- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split
- https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/length
Find the Longest Word in a String-freecodecamp算法题目的更多相关文章
- freeCodeCamp:Find the Longest Word in a String
找到提供的句子中最长的单词,并计算它的长度. 函数的返回值应该是一个数字. /* 先把字符串 str 转为数组 myarr 将数组myarr中的每个元素长度转换成一个新的数组newarr 将这个数组按 ...
- #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 ...
- Spinal Tap Case -freecodecamp算法题目
Spinal Tap Case 1.要求 将字符串转换为 spinal case. Spinal case 是 all-lowercase-words-joined-by-dashes 这种形式的,也 ...
- Map the Debris -freecodecamp算法题目
Map the Debris 1.要求 返回一个数组,其内容是把原数组中对应元素的平均海拔转换成其对应的轨道周期. 原数组中会包含格式化的对象内容,像这样 {name: 'name', avgAlt: ...
- Search and Replace -freecodecamp算法题目
Search and Replace 1.要求 使用给定的参数对句子执行一次查找和替换,然后返回新句子. 第一个参数是将要对其执行查找和替换的句子. 第二个参数是将被替换掉的单词(替换前的单词). 第 ...
- [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 ...
随机推荐
- hyperledger fabric 1.0.5 分布式部署 (八)
gdb debug peer 程序 在开始我们从 github 上download 下来的源码包,实际上已经包含了可执行的 peer 程序,但是该程序是使用 release 方式编译的,并不支持gdb ...
- HDU-3072-IntelligenceSystem(tarjan,贪心)
链接:https://vjudge.net/problem/HDU-3072 题意: 给你n个点,1个点到另一个点连接花费c,但是如果几个点可以相互可达,则这几个点连通花费为0. 求将整个图连通的最小 ...
- Codeforces 161A(贪心)
要点 我在想贪心是对的那要二分图何用,自己的想法是:二分图最开始并不知道怎么匹配最好所以就按输入顺序连了,之后慢慢修改:而这道匹配也成对匹配但从一开始你就可以知道选哪个最划算,就是贪心地选最小的.不必 ...
- CSS——三种页面引入方法
目的:为了把样式和内容分开,并且使网页元素更加丰富,引入了CSS CSS页面引入有三种方式: 1)内联式:比较不常用,因为内容和样式仍然在一起,不方便.示例: <!DOCTYPE html> ...
- 转 dos 下的 find 和 重定向 and 删除
1.find /i "ora-" *.* > check.log del /Q .\log\*.* 附录: 我对findstr是如此的依赖,以至于当我向各位讲解find命令的 ...
- 关于报错“More than one fragment with the name [spring_web] was found. This is not legal ...”的解决办法
最近在搭建一个spring mvc 项目时遇到“More than one fragment with the name [spring_web] was found. This is not leg ...
- Bootstrap下拉菜单相关
1.实现普通下拉菜单:.dropdown>button.dropdown-toggle[data-toggle="dropdown"]+ul.dropdown-menu; 2 ...
- {Linux} boot仅剩余XX字节
1. 查看已安装的linux-image各版本 dpkg --get-selections |grep linux-image 2. 查看我们当前使用的是哪一个版本: uname -a 3. ...
- django之基于cookie和装饰器实现用户认证
示例1 # Create your views here. user = "a" pwd = "a" def login(request): if reques ...
- jsp另外五大内置对象之response-设置头信息
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...