js遞歸遍歷


js遞歸實現方式:
1,遞歸就是在函數體內調用自己;
2,每個遞歸都有一個結束遞歸的條件,避免死循環 
    var fdd = function (a) {
      if (a <= 1) {
        // 終止遞歸條件
        return 1;
      } else {
        // 遞歸體
        return a * fdd(a - 1);
      }
    };
    console.log(fdd(3));

    var arrList = [1, 2, 3, 5, 100, 500, 10000, 10000, 1000, 10000002];
    function recursive() {
      console.time("遞歸遍歷");
      const testFun = function (i) {
        console.log(((arrList[i] + arrList[i]) * 5 - arrList[i]) / arrList[i]);
        if (i == arrList.length - 1) {
          return;
        }
        i++;
        testFun(i)
      }
      testFun(0)
      console.timeEnd('遞歸遍歷')
    }
    recursive()

 

參考:https://www.cnblogs.com/hellofangfang/p/13395398.html

https://www.jianshu.com/p/a69a21495214(內心)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM