Keep track of how many times a recursive function was called

后端 未结 9 1902
醉梦人生
醉梦人生 2021-02-02 05:03



        
9条回答
  •  有刺的猬
    2021-02-02 06:01

    You should add a counter argument to your function definition:

    function singleDigit(num, counter = 0) {
        console.log(`called ${counter} times`)
        //...
        return singleDigit(number, counter+1)
    }
    singleDigit(39)
    

提交回复
热议问题