Here is an object with a method that will return the factorial of a number via recursion. What I can't wrap my head around is where the value of the factorialized
variable is being stored when the function calls itself again and multiplies inputNumber
by itself -1
. Could someone explain this to me?
const Calculate = {
factorial(inputNumber) {
if (inputNumber < 2) { return 1 };
const factorialized = inputNumber * this.factorial(inputNumber - 1);
return factorialized;
}
}
Calculate.factorial(5);
Many language has Memory Heap
to save value for their operations, etc.
Image your function will execute 3 times:
Memory Heap