Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

话不多说先上代码

function __FIND_UP_NUM(i) {
        upNum = i
        if (i === 0) {
          return 0
        } else {
          if (i != 0) {
            __FIND_UP_NUM(i - 1)
          } else {
            return upNum * 1 - 1
          }
        }
      }
      __FIND_UP_NUM(2)

诡异的问题描述: 上述函数作因传入的值为2 因此会进入递归再次调用自己本身 通过调试器调试当递归传入值为0的时候确实也能进入到相应判断 期待代码应该返回的值是0 但是实际返回的确实 undefined 实在想不明白 请高手帮忙看下 谢谢啦


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
423 views
Welcome To Ask or Share your Answers For Others

1 Answer

image.png

这个修改后的代码肯定是个递归,但是返回的永远是 0


function findUpNumber(i) {
    if (i === 0) {
        return 0;
    }
    return findUpNumber(i - 1);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...