Keep track of how many times a recursive function was called

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



        
9条回答
  •  醉话见心
    2021-02-02 05:41

    Why not make a call to console.count in your function ?

    Edit: Snippet to try in your browser :

    function singleDigit(num) {
        console.count("singleDigit");
    
        let counter = 0
        let number = [...num + ''].map(Number).reduce((x, y) => {return x * y})
    
        if(number <= 9){
            console.log(number)
        }else{
            console.log(number)
            return singleDigit(number), counter += 1
        }
    }
    singleDigit(39)
    

    I have it working in Chrome 79 and Firefox 72

提交回复
热议问题