1.

What are promises JavaScript?

Answer»

Recursion is a technique for iterating over an operation by having a function call itself repeatedly until it arrives at a result.
Now, in recursive function there are two parts. One is termination condition and other is the recursion itself.  The termination condition is very important or else the recursion never stops and goes into infinite loop. 

Let consider the below simple example to add numbers from the CURRENT passed number BACKWARDS till 1. Say we pass 3, then 3+2+1 = 6. In the below example If(n≤0) is the termination condition and return n + add(n-1); is the recursion. 

The recursion works as shown in the diagram below.
It works like loop, so the first call will go to recursion part and GIVE “3 + add(2)”. 

Now the add(2) will be called and will be expended into “2 + add(1)”.
After that add(1) will be called and expanded into “1 + add(0)”. 

Finally the add(0) will trigger the termination condition If(n≤0) and produce 0.
After this everything will be added 3 + 2 + 1 + 0 to give 6.



Discussion

No Comment Found