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(内心)

posted @ 2021-01-26 21:12  云悠  阅读(1344)  评论(0编辑  收藏  举报