How exactly does this recursive function work in JavaScript?

前端 未结 7 1218
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 19:27

I have the following example of a recursive function, and what I don\'t understand is the order in which things are happening:

function power(base, exponent)         


        
相关标签:
7条回答
  • 2020-12-10 19:54

    It is generally helpful in understanding recursive functions such as this to work things out like you would in an algebra class. Consider:

    power(3, 4) 
    = 3 * power(3, 3)
    = 3 * (3 * power(3, 2))
    = 3 * (3 * (3 * power(3, 1)))
    = 3 * (3 * (3 * (3 * power(3, 0))))
    = 3 * (3 * (3 * (3 * 1)))
    = 3 * (3 * (3 * 3))
    ...
    = 81
    
    0 讨论(0)
提交回复
热议问题