solution on Find the longest word and return its length
找出字符串(可能是一句話)中最長(zhǎng)的單詞并且將其長(zhǎng)度輸出酪我。這個(gè)算法其實(shí)就是讓我們看看字符串中有多少個(gè)詞腕侄,每個(gè)詞有多少個(gè)字母权埠,然后對(duì)這些詞進(jìn)行比較颓屑,找出字母數(shù)最多的那個(gè)詞,并且返回這個(gè)最長(zhǎng)字符數(shù)單詞的長(zhǎng)度。
步驟分析
- 先把字符串str轉(zhuǎn)為數(shù)組
- 將數(shù)組的每個(gè)元素長(zhǎng)度轉(zhuǎn)換成一個(gè)新的數(shù)組
- 將這個(gè)數(shù)組由小到大排序
- 取此數(shù)組中的最后的數(shù)組,也就是最長(zhǎng)的字符串
- 將這個(gè)長(zhǎng)度值返回
JavaScript方法
在寫findLongestWord(str)函數(shù)實(shí)現(xiàn)文章開頭所述的功能忧勿,根據(jù)上面的實(shí)現(xiàn)思路,講大致會(huì)用到下面有關(guān)于JavaScript點(diǎn):
- for
- String.prototype.split()
- Array.prototype.sort()
- Array.prototype.reduce()
- Math.max()
- Array.prototype.pop()
其中 String.prototype.split() 主要是用來將字符串str轉(zhuǎn)換成數(shù)組arr瞻讽。比如:
"May the force be with you".split(" ")
//["May","the","force","be","with","you"]
這里需要注意鸳吸,在使用split()方法時(shí),""中間的要有一個(gè)空格(" ")速勇。否則將會(huì)轉(zhuǎn)變成這樣一個(gè)數(shù)組:
"May the force be with you".split("");
// ["M", "a", "y", " ", "t", "h", "e", " ", "f", "o", "r", "c", "e", " ", "b", "e", " ", "w", "i", "t", "h", " ", "y", "o", "u"]
Math.max()方法可以很簡(jiǎn)單的從一個(gè)數(shù)組中取出最大的那個(gè)值
Array.prototype.max = function(){
return Math.max.apply({},this);
}
var arr=[1,45,23,,3,6,3,4,564,45];
arr.max();//564
測(cè)試用例
通過測(cè)試用例是最好的檢測(cè)方法晌砾,檢測(cè)自己寫的函數(shù)是否符合要求,下面提供幾個(gè)測(cè)試用例:
- 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
實(shí)現(xiàn)方法
for循環(huán)
function findLongestWord(str) {
// 第1步:將傳給str的值"May the force be with you"轉(zhuǎn)換成數(shù)組
var array = str.split(' ');
// 得到數(shù)組 ["May", "the", "force", "be", "with", "you"]
var longest = 0;
// 第2步:對(duì)數(shù)組array做遍歷烦磁,并且將符合條件的值賦值給longest
for (var i = 0; i < array.length; i++) {
// array.length = 6
if (array[i].length > longest) {
// 如果為true,longest值為array[i].length;
longest = array[i].length;
}
}
/* * array = ["May", "the", "force", "be", "with", "you"]
* array.length = 6 *
* 遍歷次數(shù) i = ? i < array.length i++ array[i].length longest array[i].length > longest longest= array[i].length
* 1st 0 yes 1 "May".length=3 0 3 > 0 => yes 3
* 2nd 1 yes 2 "the".length=3 3 3 > 3 => no 3
* 3rd 2 yes 3 "force".length=5 3 5 > 3 => yes 5
* 4th 3 yes 4 "be".length=2 5 2 > 5 => no 5
* 5th 4 yes 5 "with".length=4 5 4 > 5 => no 5
* 6th 5 yes 6 "you".length=3 5 3 > 5 => no 5
* 7th 6 no * End Loop */
* // 第3步:輸出最長(zhǎng)詞的長(zhǎng)度
* return longest; // 5 }
你可以把前面的測(cè)試示例都運(yùn)行一回养匈,看看是否得到的值是正確的,如果不是个初,那就要檢查一下代碼是不是哪里出錯(cuò)了乖寒。除了上述的方法之外猴蹂,還可以for循環(huán)之后院溺,通過Math.max()直接將數(shù)組arrNum中的最大值取出。如下所示:
function findLongestWord(str){
// 1st:將傳給str的值為:"May the force be with you"轉(zhuǎn)成數(shù)組
// 得到數(shù)組arr=["May","the","force","be","with","you"]
var arrNum[];
// 2st:對(duì)數(shù)組arr做遍歷
for(var i=o; i<arr.length; i++){
// 講數(shù)組arr中每個(gè)元素的長(zhǎng)度length放到一個(gè)新數(shù)組arrNum中
arrNum.push(arr[i].length);
/*
* 遍歷次數(shù) i = ? i < arr.length i++ arr[i] arr[i].length arrNum
* 1st 0 yes 1 "May" 3 [3]
* 2nd 1 yes 2 "the" 3 [3,3]
* 3rd 2 yes 3 "force" 5 [3,3,5]
* 4th 3 yes 4 "be" 2 [3,3,5,2]
* 5th 4 yes 5 "with" 4 [3,3,5,2,4] * 6th 5 yes 6 "you" 3 [3,3,5,2,4,3]
* 7th 6 no * End Loop
*/
}
// 3st:通過Math.max()取出數(shù)組arrNum中最大值磅轻,并且輸出
return Math.max.apply(null,arrNum);//5
}
sort()方法
function findLongestWord(str){
// 1st:將傳給str的值為:"May the force be with you"轉(zhuǎn)成數(shù)組
var strArr=str.split(" ");
//得到數(shù)組arr=["May","the","force","be","with","you"]
var numArr=[];
var sortArr=[];
// 2st: 對(duì)strArr數(shù)組做遍歷珍逸,并把數(shù)組中每個(gè)詞的length放到新數(shù)組numArr中
for(var i=0; i<strArr.length; i++){
numArr[i]=strArr[i].length;
}
// 新數(shù)組:numArr=[3,3,5,2,4,3]
// 3st: 使用sort()對(duì)數(shù)組numArr排序,有小到大
sortArr=numArr.sort(funtion compare(a,b){
return a-b;
});
// 排序后的數(shù)組:sortArr=[2,3,3,3,4,5]
// 4st: 通過pop()取到數(shù)組sortArr中最后一個(gè)值聋溜,賦值給longestWord
var longestWord=sortArr.pop();//5
// 5st: 輸出longestWord
return longestWord;//5
}
或者
function findLongestWord(str) {
// 第1步:將傳給str的值為:"May the force be with you"轉(zhuǎn)成數(shù)組
var arr = str.split(' ');
// 得到數(shù)組 arr = ["May", "the", "force", "be", "with", "you"]
// 第2步: 通過sort()將數(shù)組arr按由小到大排序
arr = arr.sort(function(a, b) {
return a.length - b.length;
});
/*
* a b b.length a.length arr
* "May" "the" 3 3 ["May","the"]
* "the" "force" 5 3 ["May","the","force"]
* "force" "be" 2 5 ["be","May","the","force"]
* "be" "with" 4 2 ["be","May","the","with","force"]
* "with" "you" 3 4 ["be","May","the","you","with","force"]
*/
// 最長(zhǎng)的字符排在數(shù)組最后
// 第3步:獲取數(shù)組最長(zhǎng)字符的長(zhǎng)度
var longestString = arr.pop().length; // 5
// 第4步:返回最長(zhǎng)字符的長(zhǎng)度 return longestString; // 5 }
reduce()方法
function findLongestWord(str){
// 1st: 將傳給str的值為:"May the force be with you"轉(zhuǎn)成數(shù)組
var strSplit=str.split(' ');
//得到數(shù)組 arr = ["May", "the", "force", "be", "with", "you"];
// 2st: 使用reduce方法谆膳,取得strSplit數(shù)組中最長(zhǎng)的元素
var longestWord = strSplit.reduce(function(longest,currentWord){
return currentWord.length > longestWord ? current : longest;},"");
// 取到最長(zhǎng)的元素longestWord = "force"
/*
* strSplit = ["May", "the", "force", "be", "with", "you"];
* currentWord longest currentWord.length longest.length currentWord.length > longest.length longestWord
* "May" "" 3 0 yes "May"
* "the" "May" 3 3 no "May"
* "force" "May" 5 3 yes "force"
* "be" "force" 2 5 no "force"
* "with" "force" 4 5 no "force"
* "you" "force" 3 5 no "force"
*/
// 第3步. 返回longestWord的length
return longestWord.length;//5
// longestWord.length => "force".length =>5
}
也可以使用reduce()方法和Math.max()方法配合使用:
function findLongestWord(str){
// 1st: 將傳給str的值為:"May the force be with you"轉(zhuǎn)成數(shù)組
var strSplit = str.split(' ');
// 得到數(shù)組strSplit = ["May", "the", "force", "be", "with", "you"];
// 2st: 使用reduce方法,取到strSplit數(shù)組中最長(zhǎng)的元素length撮躁,并且返回
return strSplit.reduce(function(longest,currentWord){
return Math.max(longest, cunrrentWord.length),0);// "force".length => 5
}
總結(jié)
文章中通過幾種不同的方法實(shí)現(xiàn)了:找出字符串(可能是一句話)中最長(zhǎng)的單詞并且將其長(zhǎng)度輸出漱病。主要使用了for、sort()和reduce()方法,當(dāng)然在具體實(shí)現(xiàn)功能過程中杨帽,還運(yùn)用到了JavaScript中的split()漓穿、pop()和Math.max()等方法。