 # Recursion Replacement definition

Please can someone explain recursion definition/explainer to me? Also, what do the arrows in the example code do? Further, I don’t understand why it says it will continue until n === 1 yet it comes to 6.

4 posts were merged into an existing topic: Recursion Replacement explainer

Please can someone explain recursion definition/explainer to me? Also, what do the arrows in the example code do? Further, I don’t understand why it says it will continue until n === 1 yet it comes to 6.

function x(number) {
if (number === 1) {
return 1;
} else {
return number * x(number - 1)
} }

Let’s analyse
Let’s say number is 5
This function will basically multiply number by x(number - 1)

That is 5 * x(5-1)
That is 5 * x(4). -------(1)
But what is x(4)?
It is 4 * x(4-1)
That is 4 * x(3) ------------(2)
But what is x(3)?
It is 3 * x(3-1)
That is 3 * x(2). ------------(3)
But what is x(2)?
It is 2 * x(2-1)
That is 2 * x(1) ------------(4)

the function stops at 1 because of
if (number === 1)
And returns 1
That is 2 * 1-------------(5)

So from 1 2 3 4 5
The end result is
5 * 2 * 3 * 2 * 1
Which is equals to 120

This explaination might be difficult to understand. Sorry for that. Hope this helps

So is the point to print 120 or
5
4
3
2
1?

5 * 4 * 3 * 2 * 1 means
5×4×3×2×1 = 120