Recursive function

Tell us what’s happening:
i’m new at recursive functions, I just want o know how does the function knows when to stop running? I mean, when num - 1 = 1 the function just stop and never goes to num - 1 = 0. I want to know why, I hope i was clear. thanks

Your code so far

function factorialize(num) {
if(num === 0) return 1;
//esta é uma função recursiva, ou seja, é um função que chama a si mesma, neste caso estamos basicamente retornando o número passado multiplicado pela função, porém dessa vez o valor passado como parâmetro é num - 1. A função é executada dentro de si mesma
return num * factorialize(num - 1);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.

Challenge: Factorialize a Number

Link to the challenge:

Read through all the answers to this question. Link. Hopefully it will help you understand.

1 Like

In recursive functions, first, a terminating condition is provided then the recursion takes place.

The termination condition is the case you know will surely occur and stops the function from going further deep.

If the termination condition never occur then your program will simply go into an infinite loop.

The link provided by @nibble will be more helpful

1 Like

when factorialize(0) is called you get the return statement above.

You can see that all functions all called by adding console.log("num is " + num) as first line inside the function, you will see that you get all the expected numbers: 5,4,3,2,1,0

1 Like