js怎样查找字符串的最长单词?教你3个技巧
Admin 2021-05-10 群英技术资讯 834 次浏览
js如何查找字符串的最长单词?这篇文章给大家分享是关于基于Free Code Camp基本算法脚本来查找字符串的最长单词,本文会介绍三种方法,是小编比较推荐的,有需要的朋友可以看一看。
在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长度。
在本文中,我将解释三种方法。首先使用FOR循环,其次使用sort()方法,第三次使用reduce()方法。
算法挑战
提供的测试用例
function findLongestWord(str) { return str.length; } findLongestWord("The quick brown fox jumped over the lazy dog");
1.使用FOR循环查找最长的单词
对于此解决方案,我们将使用String.prototype.split()方法
我们将需要在split()方法的括号之间添加一个空格
var strSplit = “The quick brown fox jumped over the lazy dog”.split(‘ ‘);
它将输出一个由单词组成的数组:
var strSplit = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”];
如果不在括号中添加空格,则将得到以下输出:
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”];
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");
没有注释:
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()方法按照某种排序标准对数组进行排序,然后返回此数组的第一个元素的长度。
就我们而言,如果我们只是对数组排序
var sortArray = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”].sort();
我们将得到以下输出:
var sortArray = [“The”, “brown”, “dog”, “fox”, “jumped”, “lazy”, “over”, “quick”, “the”];
在Unicode中,数字在大写字母之前,而在小写字母之前。
我们需要按照某种排序标准对元素进行排序
[].sort(function(firstElement, secondElement) { return secondElement.length ― firstElement.length; })
比较第二个元素的长度和数组中第一个元素的长度。
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");
没有注释:
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(function(previousValue, currentValue) {...}, “”);
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");
没有注释:
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");
以上就是关于js查找字符串中最常单词的方法介绍,上述代码示例有一定的借鉴价值,有需要的朋友可以参考一下,希望文本对大家学习有帮助,想要了解更多js查找字符串的内容大家可以继续关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录前言Promise.all基础学习Iterator 接口参数思路分析源码实现Promise.allSettled基础学习思路分析源码实现Promise.race基础学习思路分析源码实现Promise.any基础学习思路分析源码实现前言恰逢 Promise 也有四个很像的静态三兄弟(Promise.all、Promi
这篇文章给大家分享的是微信小程序中的页面左右滑动效果的实现,这个效果的应用还是比较多的,小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的非常详细,感兴趣的朋友接下来一起跟随小编看看吧。
node实现定时任务的方法:1、利用setTimeOut和event事件进行管理;2、对所有加入的事件进行排序,并且计算当前时间和最近一个事件发生时间的时间间隔;3、调用setTimeOut设置回调即可。
图像优化对于网站建设和优化来说,是很重要的一部分,能大大提高网站的性能。对此,本文就给大家来分享一下JPEG/JPG、PNG、WebP这些常见的图片怎样优化,这些图像的应用场景及优缺点?
本篇文章给大家带来了关于javascript的相关知识,其中主要介绍了关于JavaScript中数值方法和属性的相关问题,包括了toSting、toFixed、toPrecision等等内容,下面一起来看一下,希望对大家有帮助。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008