• 售前

  • 售后

热门帖子
入门百科

在JavaScript中查找字符串中最长单词的三种方法(推荐)

[复制链接]
ty实话实说2017 显示全部楼层 发表于 2021-10-25 19:26:32 |阅读模式 打印 上一主题 下一主题
本文基于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. function findLongestWord(str) {
  2. return str.length;
  3. }
  4. 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. var strSplit =
  2. [“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. function findLongestWord(str) {
  2. // Step 1. Split the string into an array of strings
  3. var strSplit = str.split(' ');
  4. // var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
  5. // var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
  6. // Step 2. Initiate a variable that will hold the length of the longest word
  7. var longestWord = 0;
  8. // Step 3. Create the FOR loop
  9. for(var i = 0; i < strSplit.length; i++){
  10. if(strSplit[i].length > longestWord){ // If strSplit[i].length is greater than the word it is compared with...
  11. longestWord = strSplit[i].length; // ...then longestWord takes this new value
  12.   }
  13. }
  14. /* Here strSplit.length = 9
  15.   For each iteration: i = ? i < strSplit.length? i++ if(strSplit[i].length > longestWord)? longestWord = strSplit[i].length
  16.   1st iteration:  0    yes    1 if("The".length > 0)? => if(3 > 0)?  longestWord = 3
  17.   2nd iteration:  1    yes    2 if("quick".length > 3)? => if(5 > 3)? longestWord = 5
  18.   3rd iteration:  2    yes    3 if("brown".length > 5)? => if(5 > 5)? longestWord = 5
  19.   4th iteration:  3    yes    4 if("fox".length > 5)? => if(3 > 5)?  longestWord = 5
  20.   5th iteration:  4    yes    5 if("jumped".length > 5)? => if(6 > 5)? longestWord = 6
  21.   6th iteration:  5    yes    6 if("over".length > 6)? => if(4 > 6)? longestWord = 6
  22.   7th iteration:  6    yes    7 if("the".length > 6)? => if(3 > 6)?  longestWord = 6
  23.   8th iteration:  7    yes    8 if("lazy".length > 6)? => if(4 > 6)? longestWord = 6
  24.   9th iteration:  8    yes    9 if("dog".length > 6)? => if(3 > 6)?  longestWord = 6
  25.   10th iteration:  9    no   
  26.   End of the FOR Loop*/
  27. //Step 4. Return the longest word
  28. return longestWord; // 6
  29. }
  30. findLongestWord("The quick brown fox jumped over the lazy dog");
复制代码
没有注释:
  1. function findLongestWord(str) {
  2. var strSplit = str.split(' ');
  3. var longestWord = 0;
  4. for(var i = 0; i < strSplit.length; i++){
  5. if(strSplit[i].length > longestWord){
  6. longestWord = strSplit[i].length;
  7.   }
  8. }
  9. return longestWord;
  10. }
  11. 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. function findLongestWord(str) {
  2. // Step 1. Split the string into an array of strings
  3. var strSplit = str.split(' ');
  4. // var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
  5. // var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
  6. // Step 2. Sort the elements in the array
  7. var longestWord = strSplit.sort(function(a, b) {
  8. return b.length - a.length;
  9. });
  10. /* Sorting process
  11. a   b   b.length  a.length  var longestWord
  12. "The"  "quick"   5   3   ["quick", "The"]
  13. "quick" "brown"   5   5   ["quick", "brown", "The"]
  14. "brown" "fox"    3   5   ["quick", "brown", "The", "fox"]
  15. "fox"  "jumped"   6   3   ["jumped", quick", "brown", "The", "fox"]
  16. "jumped" "over"    4   6   ["jumped", quick", "brown", "over", "The", "fox"]
  17. "over"  "the"    3   4   ["jumped", quick", "brown", "over", "The", "fox", "the"]
  18. "the"  "lazy"    4   3   ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the"]
  19. "lazy"  "dog"    3   4   ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the", "dog"]
  20. */
  21. // Step 3. Return the length of the first element of the array
  22. return longestWord[0].length; // var longestWord = ["jumped", "quick", "brown", "over", "lazy", "The", "fox", "the", "dog"];
  23.         // longestWord[0]="jumped" => jumped".length => 6
  24. }
  25. findLongestWord("The quick brown fox jumped over the lazy dog");
复制代码
没有注释:
  1. function findLongestWord(str) {
  2. var longestWord = str.split(' ').sort(function(a, b) { return b.length - a.length; });
  3. return longestWord[0].length;
  4. }
  5. findLongestWord("The quick brown fox jumped over the lazy dog");
复制代码
3.利用reduce()方法找到最长的单词
对于此办理方案,我们将利用Array.prototype.reduce()。
      
  • reduce()的方法应用于对一个储液器的功能和所述阵列的每一值(从左到右),以将其降低到一个值。
reduce()对数组中存在的每个元素执行一次回调函数。
您可以提供一个初始值作为要减少的第二个参数,这里我们将添加一个空字符串“”。
  1. [].reduce(function(previousValue, currentValue) {...}, “”);
复制代码
  1. function findLongestWord(str) {
  2. // Step 1. Split the string into an array of strings
  3. var strSplit = str.split(' ');
  4. // var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
  5. // var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
  6. // Step 2. Use the reduce method
  7. var longestWord = strSplit.reduce(function(longest, currentWord) {
  8. if(currentWord.length > longest.length)
  9.   return currentWord;
  10. else
  11.   return longest;
  12. }, "");
  13. /* Reduce process
  14. currentWord  longest  currentWord.length  longest.length if(currentWord.length > longest.length)? var longestWord
  15. "The"    ""     3      0        yes       "The"
  16. "quick"   "The"    5      3        yes       "quick"
  17. "brown"   "quick"    5      5        no       "quick"
  18. "fox"    "quick"    3      5        no       "quick"
  19. "jumped"   "quick"    6      5        yes       "jumped"
  20. "over"   "jumped"   4      6        no       "jumped"
  21. "the"    "jumped"   3      6        no       "jumped"
  22. "lazy"   "jumped"   4      6        no       "jumped"
  23. "dog"    "jumped"   3      6        no       "jumped"
  24. */
  25. // Step 3. Return the length of the longestWord
  26. return longestWord.length; // var longestWord = "jumped"
  27.         // longestWord.length => "jumped".length => 6
  28. }
  29. findLongestWord("The quick brown fox jumped over the lazy dog");
复制代码
没有注释:
  1. function findLongestWord(str) {
  2. var longestWord = str.split(' ').reduce(function(longest, currentWord) {
  3. return currentWord.length > longest.length ? currentWord : longest;
  4. }, "");
  5. return longestWord.length;
  6. }
  7. findLongestWord("The quick brown fox jumped over the lazy dog");
复制代码
到此这篇关于在JavaScript中查找字符串中最长单词的三种方法的文章就介绍到这了,更多相关js查找字符串最长单词内容请搜索脚本之家以前的文章或继承欣赏下面的相关文章希望大家以后多多支持脚本之家!

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作