一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - js教程 - 在JavaScript中查找字符串中最長單詞的三種方法(推薦)

在JavaScript中查找字符串中最長單詞的三種方法(推薦)

2022-01-04 16:25Hunter網絡安全 js教程

這篇文章主要介紹了在JavaScript中查找字符串中最長單詞的三種方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

本文基于Free Code Camp基本算法腳本“查找字符串中最長的單詞”。

在此算法中,我們要查看每個單詞并計算每個單詞中有多少個字母。然后,比較計數以確定哪個單詞的字符最多,并返回最長單詞的長度。

在本文中,我將解釋三種方法。首先使用FOR循環,其次使用sort()方法,第三次使用reduce()方法。

算法挑戰

  • 返回提供的句子中最長單詞的長度。
  • 您的回復應該是一個數字。

提供的測試用例

  • findLongestWord(“The quick brown fox jumped over the lazy dog”)返回一個數字
  • findLongestWord(“The quick brown fox jumped over the lazy dog”)返回6
  • findLongestWord(“May the force be with you”)返回5
  • findLongestWord(“Google do a barrel roll”)返回6
  • findLongestWord(“What is the average airspeed velocity of an unladen swallow”)返回8
  • findLongestWord(“What if we try a super-long word such as otorhinolaryngology”)返回19
?
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()的方法通過分離串分成子串分割字符串對象到字符串數組。

我們將需要在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()方法按照某種排序標準對數組進行排序,然后返回此數組的第一個元素的長度。

  • 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()的方法應用于對一個儲液器的功能和所述陣列的每一值(從左到右),以將其降低到一個值。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美腐剧mm在线观看 | 大ji吧快给我别停受不了视频 | 国产精品激情综合久久 | 99pao在线视频精品免费 | 999资源站 | 我的奶头被客人吸的又肿又红 | 亚洲人成绝费网站色ww | 国产乱子伦一区二区三区 | 三级小说网站 | 色999| 91精品国产品国语在线不卡 | 乳环贵妇堕落开发调教番号 | 成年女人毛片免费观看97 | 被黑人同学彻底征服全文小说阅读 | 美女尿口照片 | 国产精品一区二区在线观看完整版 | 亚洲 日本 中文字幕 制服 | 日韩一级片在线播放 | 动漫精品一区二区三区3d | gay中国 | 亚洲第一成年免费网站 | 久久三级网站 | 国产成人精品免费久久久久 | 国产肥女bbwbbw | 久久精品视频uu | 97久久精品午夜一区二区 | 91精品久久 | re99| 亚洲AV无码国产精品色午夜情 | 国内自拍2019 | 久久er99热精品一区二区 | 国自产精品手机在线视频 | 精品久久久久久国产91 | 欧美亚洲高清日韩成人 | 亚洲成年人免费网站 | 九九精品视频在线观看九九 | 99久久国产亚洲综合精品 | 欧美成人aa | 无限资源在线观看完整版免费下载 | 国产成人综合亚洲一区 | 欧美精品1区2区 |