- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章在JavaScript中查找字符串中最长单词的三种方法(推荐)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文基于Free Code Camp基本算法脚本“查找字符串中最长的单词”.
在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长度.
在本文中,我将解释三种方法。首先使用FOR循环,其次使用sort()方法,第三次使用reduce()方法.
算法挑战 。
提供的测试用例 。
1
2
3
4
|
function
findLongestWord(str) {
return
str.length;
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
1.使用FOR循环查找最长的单词 。
对于此解决方案,我们将使用String.prototype.split()方法 。
我们将需要在split()方法的括号之间添加一个空格 。
1
|
var
strSplit = “The quick brown fox jumped over the lazy dog”.split(‘ ‘);
|
它将输出一个由单词组成的数组:
1
|
var
strSplit = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”];
|
如果不在括号中添加空格,则将得到以下输出:
1
2
|
var
strSplit =
[“T”, “h”, “e”, “ “, “q”, “u”, “i”, “c”, “k”, “ “, “b”, “r”, “o”, “w”, “n”, “ “, “f”, “o”, “x”, “ “, “j”, “u”, “m”, “p”, “e”, “d”, “ “, “o”, “v”, “e”, “r”, “ “, “t”, “h”, “e”, “ “, “l”, “a”, “z”, “y”, “ “, “d”, “o”, “g”];
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
function
findLongestWord(str) {
// Step 1. Split the string into an array of strings
var
strSplit = str.split(
' '
);
// var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
// var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
// Step 2. Initiate a variable that will hold the length of the longest word
var
longestWord = 0;
// Step 3. Create the FOR loop
for
(
var
i = 0; i < strSplit.length; i++){
if
(strSplit[i].length > longestWord){
// If strSplit[i].length is greater than the word it is compared with...
longestWord = strSplit[i].length;
// ...then longestWord takes this new value
}
}
/* Here strSplit.length = 9
For each iteration: i = ? i < strSplit.length? i++ if(strSplit[i].length > longestWord)? longestWord = strSplit[i].length
1st iteration: 0 yes 1 if("The".length > 0)? => if(3 > 0)? longestWord = 3
2nd iteration: 1 yes 2 if("quick".length > 3)? => if(5 > 3)? longestWord = 5
3rd iteration: 2 yes 3 if("brown".length > 5)? => if(5 > 5)? longestWord = 5
4th iteration: 3 yes 4 if("fox".length > 5)? => if(3 > 5)? longestWord = 5
5th iteration: 4 yes 5 if("jumped".length > 5)? => if(6 > 5)? longestWord = 6
6th iteration: 5 yes 6 if("over".length > 6)? => if(4 > 6)? longestWord = 6
7th iteration: 6 yes 7 if("the".length > 6)? => if(3 > 6)? longestWord = 6
8th iteration: 7 yes 8 if("lazy".length > 6)? => if(4 > 6)? longestWord = 6
9th iteration: 8 yes 9 if("dog".length > 6)? => if(3 > 6)? longestWord = 6
10th iteration: 9 no
End of the FOR Loop*/
//Step 4. Return the longest word
return
longestWord;
// 6
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
没有注释:
1
2
3
4
5
6
7
8
9
10
11
|
function
findLongestWord(str) {
var
strSplit = str.split(
' '
);
var
longestWord = 0;
for
(
var
i = 0; i < strSplit.length; i++){
if
(strSplit[i].length > longestWord){
longestWord = strSplit[i].length;
}
}
return
longestWord;
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
2.使用sort()方法找到最长的单词 。
对于此解决方案,我们将使用Array.prototype.sort()方法按照某种排序标准对数组进行排序,然后返回此数组的第一个元素的长度.
就我们而言,如果我们只是对数组排序 。
1
|
var
sortArray = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”].sort();
|
我们将得到以下输出:
1
|
var
sortArray = [“The”, “brown”, “dog”, “fox”, “jumped”, “lazy”, “over”, “quick”, “the”];
|
在Unicode中,数字在大写字母之前,而在小写字母之前.
我们需要按照某种排序标准对元素进行排序 。
1
|
[].sort(
function
(firstElement, secondElement) {
return
secondElement.length — firstElement.length; })
|
比较第二个元素的长度和数组中第一个元素的长度.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
function
findLongestWord(str) {
// Step 1. Split the string into an array of strings
var
strSplit = str.split(
' '
);
// var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
// var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
// Step 2. Sort the elements in the array
var
longestWord = strSplit.sort(
function
(a, b) {
return
b.length - a.length;
});
/* Sorting process
a b b.length a.length var longestWord
"The" "quick" 5 3 ["quick", "The"]
"quick" "brown" 5 5 ["quick", "brown", "The"]
"brown" "fox" 3 5 ["quick", "brown", "The", "fox"]
"fox" "jumped" 6 3 ["jumped", quick", "brown", "The", "fox"]
"jumped" "over" 4 6 ["jumped", quick", "brown", "over", "The", "fox"]
"over" "the" 3 4 ["jumped", quick", "brown", "over", "The", "fox", "the"]
"the" "lazy" 4 3 ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the"]
"lazy" "dog" 3 4 ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the", "dog"]
*/
// Step 3. Return the length of the first element of the array
return
longestWord[0].length;
// var longestWord = ["jumped", "quick", "brown", "over", "lazy", "The", "fox", "the", "dog"];
// longestWord[0]="jumped" => jumped".length => 6
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
没有注释:
1
2
3
4
5
|
function
findLongestWord(str) {
var
longestWord = str.split(
' '
).sort(
function
(a, b) {
return
b.length - a.length; });
return
longestWord[0].length;
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
3.使用reduce()方法找到最长的单词 。
对于此解决方案,我们将使用Array.prototype.reduce().
reduce()对数组中存在的每个元素执行一次回调函数.
您可以提供一个初始值作为要减少的第二个参数,这里我们将添加一个空字符串“”.
1
|
[].reduce(
function
(previousValue, currentValue) {...}, “”);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
function
findLongestWord(str) {
// Step 1. Split the string into an array of strings
var
strSplit = str.split(
' '
);
// var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
// var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
// Step 2. Use the reduce method
var
longestWord = strSplit.reduce(
function
(longest, currentWord) {
if
(currentWord.length > longest.length)
return
currentWord;
else
return
longest;
},
""
);
/* Reduce process
currentWord longest currentWord.length longest.length if(currentWord.length > longest.length)? var longestWord
"The" "" 3 0 yes "The"
"quick" "The" 5 3 yes "quick"
"brown" "quick" 5 5 no "quick"
"fox" "quick" 3 5 no "quick"
"jumped" "quick" 6 5 yes "jumped"
"over" "jumped" 4 6 no "jumped"
"the" "jumped" 3 6 no "jumped"
"lazy" "jumped" 4 6 no "jumped"
"dog" "jumped" 3 6 no "jumped"
*/
// Step 3. Return the length of the longestWord
return
longestWord.length;
// var longestWord = "jumped"
// longestWord.length => "jumped".length => 6
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
没有注释:
1
2
3
4
5
6
7
|
function
findLongestWord(str) {
var
longestWord = str.split(
' '
).reduce(
function
(longest, currentWord) {
return
currentWord.length > longest.length ? currentWord : longest;
},
""
);
return
longestWord.length;
}
findLongestWord(
"The quick brown fox jumped over the lazy dog"
);
|
到此这篇关于在JavaScript中查找字符串中最长单词的三种方法的文章就介绍到这了,更多相关js查找字符串最长单词内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/qq_25879801/article/details/111408976 。
最后此篇关于在JavaScript中查找字符串中最长单词的三种方法(推荐)的文章就讲到这里了,如果你想了解更多关于在JavaScript中查找字符串中最长单词的三种方法(推荐)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!