JavaScript实现-找出最长单词-Find the Longest Word in a String
2017-08-23 11:37
751 查看
找出最长单词
在句子中找出最长的单词,并返回它的长度。
函数的返回值应该是一个数字。
参考String.split()和String.length
function findLongestWord(str) {
// 请把你的代码写在这里
var arr = str.split(" ");
var max = 0;
for (var i = 0; i<arr.length; i++){
if (arr[i].length >= max){
max = arr[i].length;
}
}
//console.log(max);
return max;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
在句子中找出最长的单词,并返回它的长度。
函数的返回值应该是一个数字。
参考String.split()和String.length
function findLongestWord(str) {
// 请把你的代码写在这里
var arr = str.split(" ");
var max = 0;
for (var i = 0; i<arr.length; i++){
if (arr[i].length >= max){
max = arr[i].length;
}
}
//console.log(max);
return max;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
相关文章推荐
- FCC算法:四、Find the Longest Word in a String -- 找出最长单词
- Bonfire: Find the Longest Word in a String 找出一句话中最大的单词
- freecodecamp学习--Find the Longest Word in a String
- Find the Longest Word in a String
- choose the longest word in the line .把字符串中最长的单词输出
- FCC 学习日志:Find the Longest Word in a String
- freecodecamp学习--Find the Longest Word in a String
- [LeetCode] Longest Word in Dictionary through Deleting 删除后得到的字典中的最长单词
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
- [LeetCode] Longest Word in Dictionary 字典中的最长单词
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
- 《牛客网leetcode144题》Given a string S, find the longest palindromic substring in S
- Given a string S, find the longest palindromic substring in S.
- Find the longest substring with k unique distinct characters in a given string (Google)
- find the longest numeric string in the given input strings
- 找出矩阵中含有0最多的一行(find the longest row of zero)
- 找出无向图汇总的相连要素find-the-connected-component-in-the-undirected-graph
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- [CareerCup] 18.7 Longest Word 最长的单词
- Given a string, find the length of the longest substring without repeating characters