在JavaScript中查找字符串中最长单词的三种方法(推荐)
本文基于FreeCodeCamp基本算法脚本“查找字符串中最长的单词”。
在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长度。
在本文中,我将解释三种方法。首先使用FOR循环,其次使用sort()方法,第三次使用reduce()方法。
算法挑战
- 返回提供的句子中最长单词的长度。
- 您的回复应该是一个数字。
提供的测试用例
- findLongestWord(“Thequickbrownfoxjumpedoverthelazydog”)返回一个数字
- findLongestWord(“Thequickbrownfoxjumpedoverthelazydog”)返回6
- findLongestWord(“Maytheforcebewithyou”)返回5
- findLongestWord(“Googledoabarrelroll”)返回6
- findLongestWord(“Whatistheaverageairspeedvelocityofanunladenswallow”)返回8
- findLongestWord(“Whatifwetryasuper-longwordsuchasotorhinolaryngology”)返回19
functionfindLongestWord(str){ returnstr.length; } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
1.使用FOR循环查找最长的单词
对于此解决方案,我们将使用String.prototype.split()方法
- split()的方法通过分离串分成子串分割字符串对象到字符串数组。
我们将需要在split()方法的括号之间添加一个空格
varstrSplit=“Thequickbrownfoxjumpedoverthelazydog”.split(‘‘);
它将输出一个由单词组成的数组:
varstrSplit=[“The”,“quick”,“brown”,“fox”,“jumped”,“over”,“the”,“lazy”,“dog”];
如果不在括号中添加空格,则将得到以下输出:
varstrSplit= [“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”];
functionfindLongestWord(str){ //Step1.Splitthestringintoanarrayofstrings varstrSplit=str.split(''); //varstrSplit="Thequickbrownfoxjumpedoverthelazydog".split(''); //varstrSplit=["The","quick","brown","fox","jumped","over","the","lazy","dog"]; //Step2.Initiateavariablethatwillholdthelengthofthelongestword varlongestWord=0; //Step3.CreatetheFORloop for(vari=0;ilongestWord){//IfstrSplit[i].lengthisgreaterthantheworditiscomparedwith... longestWord=strSplit[i].length;//...thenlongestWordtakesthisnewvalue } } /*HerestrSplit.length=9 Foreachiteration:i=?i longestWord)?longestWord=strSplit[i].length 1stiteration:0yes1if("The".length>0)?=>if(3>0)?longestWord=3 2nditeration:1yes2if("quick".length>3)?=>if(5>3)?longestWord=5 3rditeration:2yes3if("brown".length>5)?=>if(5>5)?longestWord=5 4thiteration:3yes4if("fox".length>5)?=>if(3>5)?longestWord=5 5thiteration:4yes5if("jumped".length>5)?=>if(6>5)?longestWord=6 6thiteration:5yes6if("over".length>6)?=>if(4>6)?longestWord=6 7thiteration:6yes7if("the".length>6)?=>if(3>6)?longestWord=6 8thiteration:7yes8if("lazy".length>6)?=>if(4>6)?longestWord=6 9thiteration:8yes9if("dog".length>6)?=>if(3>6)?longestWord=6 10thiteration:9no EndoftheFORLoop*/ //Step4.Returnthelongestword returnlongestWord;//6 } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
没有注释:
functionfindLongestWord(str){ varstrSplit=str.split(''); varlongestWord=0; for(vari=0;ilongestWord){ longestWord=strSplit[i].length; } } returnlongestWord; } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
2.使用sort()方法找到最长的单词
对于此解决方案,我们将使用Array.prototype.sort()方法按照某种排序标准对数组进行排序,然后返回此数组的第一个元素的长度。
- sort()的方法进行排序的阵列的代替元素并返回数组。
就我们而言,如果我们只是对数组排序
varsortArray=[“The”,“quick”,“brown”,“fox”,“jumped”,“over”,“the”,“lazy”,“dog”].sort();
我们将得到以下输出:
varsortArray=[“The”,“brown”,“dog”,“fox”,“jumped”,“lazy”,“over”,“quick”,“the”];
在Unicode中,数字在大写字母之前,而在小写字母之前。
我们需要按照某种排序标准对元素进行排序
[].sort(function(firstElement,secondElement){returnsecondElement.length—firstElement.length;})
比较第二个元素的长度和数组中第一个元素的长度。
functionfindLongestWord(str){ //Step1.Splitthestringintoanarrayofstrings varstrSplit=str.split(''); //varstrSplit="Thequickbrownfoxjumpedoverthelazydog".split(''); //varstrSplit=["The","quick","brown","fox","jumped","over","the","lazy","dog"]; //Step2.Sorttheelementsinthearray varlongestWord=strSplit.sort(function(a,b){ returnb.length-a.length; }); /*Sortingprocess abb.lengtha.lengthvarlongestWord "The""quick"53["quick","The"] "quick""brown"55["quick","brown","The"] "brown""fox"35["quick","brown","The","fox"] "fox""jumped"63["jumped",quick","brown","The","fox"] "jumped""over"46["jumped",quick","brown","over","The","fox"] "over""the"34["jumped",quick","brown","over","The","fox","the"] "the""lazy"43["jumped",quick","brown","over","lazy","The","fox","the"] "lazy""dog"34["jumped",quick","brown","over","lazy","The","fox","the","dog"] */ //Step3.Returnthelengthofthefirstelementofthearray returnlongestWord[0].length;//varlongestWord=["jumped","quick","brown","over","lazy","The","fox","the","dog"]; //longestWord[0]="jumped"=>jumped".length=>6 } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
没有注释:
functionfindLongestWord(str){ varlongestWord=str.split('').sort(function(a,b){returnb.length-a.length;}); returnlongestWord[0].length; } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
3.使用reduce()方法找到最长的单词
对于此解决方案,我们将使用Array.prototype.reduce()。
- reduce()的方法应用于对一个储液器的功能和所述阵列的每一值(从左到右),以将其降低到一个值。
reduce()对数组中存在的每个元素执行一次回调函数。
您可以提供一个初始值作为要减少的第二个参数,这里我们将添加一个空字符串“”。
[].reduce(function(previousValue,currentValue){...},“”);
functionfindLongestWord(str){ //Step1.Splitthestringintoanarrayofstrings varstrSplit=str.split(''); //varstrSplit="Thequickbrownfoxjumpedoverthelazydog".split(''); //varstrSplit=["The","quick","brown","fox","jumped","over","the","lazy","dog"]; //Step2.Usethereducemethod varlongestWord=strSplit.reduce(function(longest,currentWord){ if(currentWord.length>longest.length) returncurrentWord; else returnlongest; },""); /*Reduceprocess currentWordlongestcurrentWord.lengthlongest.lengthif(currentWord.length>longest.length)?varlongestWord "The"""30yes"The" "quick""The"53yes"quick" "brown""quick"55no"quick" "fox""quick"35no"quick" "jumped""quick"65yes"jumped" "over""jumped"46no"jumped" "the""jumped"36no"jumped" "lazy""jumped"46no"jumped" "dog""jumped"36no"jumped" */ //Step3.ReturnthelengthofthelongestWord returnlongestWord.length;//varlongestWord="jumped" //longestWord.length=>"jumped".length=>6 } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
没有注释:
functionfindLongestWord(str){ varlongestWord=str.split('').reduce(function(longest,currentWord){ returncurrentWord.length>longest.length?currentWord:longest; },""); returnlongestWord.length; } findLongestWord("Thequickbrownfoxjumpedoverthelazydog");
到此这篇关于在JavaScript中查找字符串中最长单词的三种方法的文章就介绍到这了,更多相关js查找字符串最长单词内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!