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);
}
factorialize(5);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0.
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