js怎样查找字符串的最长单词?教你 几个窍门
发布时间:2022-04-14 13:24:19 所属栏目:语言 来源:互联网
导读:js如何查找字符串的最长单词?这篇文章给大家分享是关于基于Free Code Camp基本算法脚本来查找字符串的最长单词,本文会介绍三种方法,是小编比较推荐的,有需要的朋友可以看一看。 在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较
js如何查找字符串的最长单词?这篇文章给大家分享是关于基于Free Code Camp基本算法脚本来查找字符串的最长单词,本文会介绍三种方法,是小编比较推荐的,有需要的朋友可以看一看。 在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长度。 在本文中,我将解释三种方法。首先使用FOR循环,其次使用sort()方法,第三次使用reduce()方法。 findLongestWord("The quick brown fox jumped over the lazy dog"); 1.使用FOR循环查找最长的单词 对于此解决方案,我们将使用String.prototype.split()方法 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"); 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; }) 比较第二个元素的长度和数组中第一个元素的长度。 (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读