function singleDigit(num) { 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)
The code above takes an integer and reduces it to a single digit by multiplying it by its own digits.
Example is 39.
3 x 9 = 27. 2 x 7 = 14. 1 x 4 = 4.
The console will log:
27 14 4
How do I keep track that the recursive function was called 3 times?
I have tried adding a counter but it fails to update. Would appreciate any help
Explanation: The recursive function is called 11 times.
Thus in our case, the number of recursive calls is n0 +n2 = 2n0 −1, which means that the total number of recursive calls is equal to 2Fn+1 − 1. This way, we have reached the same result with [2], however, in a much simpler way from the mathematical and pedagogical point of view.
Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely. The Python interpreter limits the depths of recursion to help avoid infinite recursions, resulting in stack overflows. By default, the maximum depth of recursion is 1000 .
That is, the number of function calls to calculate a Fibonacci number F(n) is 2F(n)−1.
♦ When a function’s recursive call is part of the returnstatement, break the two apart by introducing an intermediate variable. This provides the opportunity to inspect the value actually being returned from the called frame.
You recursively reduce it to a single digit. You log the intermediate values, and you would like a count of the recursive calls made. One way to handle all this is to write a pure function which will return a data structure that contains the final result, the steps taken and the call count all in one:
♦ In recursion, counting frames is usually more important than counting steps. ♦ Being able to separate the pre-recursive and post-recursive state of a function (and the accompanying namespaces for variables) is essential to understanding how a recursive cascade unfolds.
use static variable inside the recursive function. static int i =0; and in the beginning of the function, say i++. every time the function is called, this i will be incremented. and if the value of i become 10, you can terminate. Show activity on this post.
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)
The traditional solution is to pass the count as a parameter to the function as suggested by another answer.
However, there is another solution in js. A few other answers suggested simply declaring count outside the recursive function:
let counter = 0 function singleDigit(num) { counter++; // .. }
This of course works. However this makes the function non-reentrant (cannot be called twice correctly). In some cases you can ignore this problem and simply make sure you don't call singleDigit
twice (javascript is single threaded so it's not too hard to do) but this is a bug waiting to happen if you update singleDigit
later to be asynchronous and it also feels ugly.
The solution is to declare the counter
variable outside but not globally. This is possible because javascript has closures:
function singleDigit(num) { let counter = 0; // outside but in a closure // use an inner function as the real recursive function: function recursion (num) { counter ++ let number = [...num + ''].map(Number).reduce((x, y) => {return x * y}) if(number <= 9){ return counter // return final count (terminate) }else{ return recursion(number) // recurse! } } return recursion(num); // start recursion }
This is similar to the global solution but each time you call singleDigit
(which is now not a recursive function) it will create a new instance of the counter
variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With