循環轉遞歸

遞歸算法應用場景:http://www.cnblogs.com/handsCool/p/4496495.html

  • 遞歸需要邊界條件,遞歸前進段,遞歸返回段
  • 遞歸條件不成立 ==》 遞歸前進
  • 遞歸條件滿足 ==》 遞歸返回
    循環 實現階乘 和 遞歸 實現 一個版本出來
  • 階乘:輸入 n! = n * (n-1) * (n-2) ..... 1
function factorial(num){
    if(num<1){
        return 1;
    }else{
        return num*factorial(num-1);
    }
}

  • n階樓梯,小張每次只能走一階或者兩階,請問走完此樓梯共有多少種方法?
 function fun(s){
      if(s==0 || s==1){
        return 1;
      }else{
      return fun(s-1)+fun(s-2);
      }
 }

 console.log(fun(4));
<select class="agt_rt_nm_ipt province" style="width: 270px" name="provinceId" data-provinceid="{{provinceId}}"></select>
<select class="agt_rt_nm_ipt city" style="width: 270px" name="cityId" data-cityid="{{cityId}}"></select>
<select class="agt_rt_nm_ipt district" style="width: 270px" name="areaId" data-areaid="{{areaId}}"></select>
<select class="agt_rt_nm_ipt street" style="width: 270px" name="streetsId" data-streestid="{{streetsId}}"></select>
 function areaSelect(province,city,district,street){
        var html = "<option>省</option>";
        $(city).append("<option>市</option>");
        $(district).append("<option>區</option>");
        $(street).append("<option>街道</option>");
        for(var i = 0;i<areaList.length;i++){
            var pp = areaList[i];
            html += "<option value='"+pp['id']+"' code='"+pp['code']+"' parentid='"+pp['parentId']+"'>"+pp['name']+"</option>";
        }
        $(province).html(html);

        $(province).change(function(){
            if($(this).val()=="")  return;
            $(city+' option').remove();
            $(district+' option').remove();
            $(street+' option').remove();
            var code = $(this).find('option:selected').attr("code");
            var html = "<option>市</option>";
            for(var i = 0;i<areaList.length;i++){
                for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                    var cc = areaList[i]['subordinates'][j];
                    if(cc['parentId'] == code){
                        html += "<option value='"+cc['id']+"' code='"+cc['code']+"' parentid='"+cc['parentId']+"'>"+cc['name']+"</option>";
                    }
                }
            }
            $(city).html(html);
        });

        $(city).change(function(){
            if($(this).val()=="") return;
            $(district+' option').remove();
            $(street+' option').remove();
            var code = $(this).find('option:selected').attr("code");
            var html = "<option>區</option>";
            for(var i = 0;i<areaList.length;i++){
                for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                    for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
                        var dd = areaList[i]['subordinates'][j]['subordinates'][k];
                        if(dd['parentId'] == code){
                            html += "<option value='"+dd['id']+"' code='"+dd['code']+"' parentid='"+dd['parentId']+"'>"+dd['name']+"</option>";
                        }
                    }
                }
            }
            $(district).html(html);
        });

        $(district).change(function(){
            if($(this).val()=="") return;
            $(street+' option').remove();
            var code = $(this).find('option:selected').attr("code");
            var html = "<option>街道</option>";
            for(var i = 0;i<areaList.length;i++){
                for(var j = 0;j<areaList[i]['subordinates'].length;j++){
                    for(var k = 0;k<areaList[i]['subordinates'][j]['subordinates'].length;k++){
                        for(var l = 0;l<areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'].length;l++){
                            var tt = areaList[i]['subordinates'][j]['subordinates'][k]['subordinates'][l];
                            if(tt['parentId'] == code){
                                html +=  "<option value='"+tt['id']+"' code='"+tt['code']+"' parentid='"+tt['parentId']+"'>"+tt['name']+"</option>";
                            }
                        }
                    }
                }
            }
            $(street).html(html);
        });
    }
    areaSelect(".province",".city",".district",".street");


    function selectOption(selector){
        var _selectorId = $("select[name="+selector+"]").attr("data-"+selector);
        var options = $("select[name="+selector+"]").children("option");
        for(var i =0;i<options.length;i++){
            if(options[i].value == _selectorId){
                options[i].selected = true;
            }
        } 
    }

    selectOption("provinceId");
    selectOption("cityId");
    selectOption("areaId");
    selectOption("streetsId");

閉包【closure】

應用場景

  • 閉包常用來實現對象的私有數據,在事件處理和回調函數中會用到
  • 偏函數,柯里化,函數式編程等

什么是閉包

  • 閉包能讓我們從一個函數內部訪問其外部函數的作用域【內部函數能訪問外部函數作用域中的變量】
  • 在JS中,閉包是一種用來實現數據私有的原生機制,當使用閉包來實現數據私有時,被封裝的變量只有在閉包容器函數作用域中使用。在閉包作用域下的公開方法才可以訪問這些數據。
  • 閉包通常是在一個函數內部創建另一個函數
  • 閉包:一個函數可以訪問不在其作用域范圍內但在其外層作用域中存在的變量,該外層作用域的頂層為全局作用域
  • 閉包不等于匿名函數
  • 閉包的目的是通過返回函數來擴大作用域
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容