js 遞迴學習

來源:互聯網
上載者:User

標籤:

作用:將一些複製的演算法變為簡單,比如:(舉例子)計算數組 var  a =[1,3,4,6,7,8]的長度;求 5!的值,也可以做搜尋用等。

          

//求數組的長度
function len(arry){ if(arry[0] == null && arry[0]==undefined) return 0; else{ arry.shift(); return 1+ len(arry) }}

//求5!
function factorial(n){
if(n == 0 ){
return 1;
}else{    
return n*factorial(n-1);
}
}

 

  

缺陷:如果遞迴函式的終止條件不明確或者缺少終止條件會導致函數長時間運行,是使用者介面處於假死狀態。

注意:瀏覽器對遞迴的支援熟練與JS調用棧大小直接相關,當使用太多遞迴甚至超過最大調用棧容量時,瀏覽器會報錯誤資訊,各個瀏覽器對報錯的提示資訊也不一樣,比如

    Google瀏覽器console中調用factorial(1000000000)則會返回Uncaught RangeError: Maximum call stack size exceeded(…),我們可以採用一些處理方法來正確處理這         些錯誤,

  

try{    factorial(100000000000000)}catch(ex){    console.log("超出最大調用棧")}

 

規則:1、知道什麼時候停止;

   2、怎樣進行下一步;

   3、將問題分解為一個步驟和較小的問題。

 

搜尋問題應用:返回test.data中 name屬性值為resultaaa的對象

var test ={
"data": [
{
"a1":[{"a11":[{"a111":{"name":"da"}},
{"a112":{"name":"da"}}]},
{"a12":[{"a121":[{"a1211":{"name":"da"}},
{"a1212":{"name":"da"}}]
}]
},
{"a13":[{"a131":{"name":"da"}},
{"a132":{"name":"da"}}
]
}
]
},{
"b1":null
},{
"c1":[{"c11":null},
{"c12":null},
{"c13":[{"c131":null},
{"c132":[{"c1321":[{"c13211":{"name":"result","companyloacation":"chengdu"}},
{"c13212":{"name":"da"}
}]
},
{"c1322":null}]
}
]}
]
}
]
}
 

  遞迴函式的實現

var diguitest ={    test:function(arr,finder){        function find(arr,finde){            if(arr == null ||arr  == undefined){                return null;//終止在該次對象上的搜尋            }            if(arr.name && arr.name == finder ){ return arr;//結束搜尋並返回對象 } //不滿足上麵條件則繼續搜尋 for(var i in arr){ if(arr[i] instanceof Object){ result = find(arr[i],finder) //如果找到則依次向上返回該對象 if(result){ return result; } } } } return find(arr,finder) }}

返回結果:

var cc =  diguitest.test(test.data, "result")Object {name: "result", companyloacation: "chengdu"}

 

 合并排序應用:將數組 var test  =[2,5,6,8,9,3,1,3,45,6]進行排序

//依次比較兩個數組中數位大小function merg(left,right){    var result=[];    while(left.length > 0 && right.length >0){        if(left[0] < right[0]){            result.push(left.shift())        }else{            result.push(right.shift())        }    }    return  result.concat(left).concat(right);}//將數組分為兩組---無限分為兩組直至不能分為兩組為止function mergSort(item){    //分組終止條件    if(item.length == 1){        return item;    }    var moddle =Math.floor(item.length/2);    var left = item.slice(0,moddle);    var right = item.slice(moddle);    return  merg(mergSort(left),mergSort(right));}

調用後返回結果:

mergSort(test)
[1, 2, 3, 3, 5, 6, 6, 8, 9, 45]

 

js 遞迴學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.