【问题标题】:Recursive function is not working properly递归函数不能正常工作
【发布时间】:2022-12-24 22:22:16
【问题描述】:

我像这样创建了递归函数。

function countDown(count){
    if(count==0){
        return;
    }
   countDown(count-1)
}

console.log(countDown(5))

该函数返回未定义的从 5 到 1 的打印数字。我做错了什么?

【问题讨论】:

    标签: javascript algorithm recursion


    【解决方案1】:

    你什么都不返回,所以很明显它将是未定义的。

    function countDown(count){
        if(count==0){
            // you should return a value if you want value instead of undefined
            return 100;
        }
       countDown(count-1)
    }
    
    console.log(countDown(5))

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 2016-02-04
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多