时间:2021-05-25
本文基于Free Code Camp基本算法脚本“查找字符串中最长的单词”。
在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长度。
在本文中,我将解释三种方法。首先使用FOR循环,其次使用sort()方法,第三次使用reduce()方法。
算法挑战
提供的测试用例
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 } } //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; }); // 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; }, ""); // 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");到此这篇关于在JavaScript中查找字符串中最长单词的三种方法的文章就介绍到这了,更多相关js查找字符串最长单词内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python实现查找字符串数组最长公共前缀。分享给大家供大家参考,具体如下:编写一个函数来查找字符串数组中的最长公共前缀。classSolutio
第一种方法:用php的strpos()函数判断字符串中是否包含某字符串的方法if(strpos('?>第四种、stristrstristr()函数查找字
本文实例讲述了php查找字符串出现次数的方法。分享给大家供大家参考。具体方法如下:在php中查找字符串出现次数的查找可以通过substr_count()函数来实
JavaScript中截取字符串有三种方法,分别是substring,substr,split,接下来将在文章中为大家详细介绍它们的使用方法。substring
下面介绍javascript实现字符串反转的三种方法: 第一种方法: 代码如下:varstr="abcdef";console.log(str.spli