π INDEX
LOADING ENGINE...
JS Recursion
"To loop is human, to recurse divine."
β 0 XP
recursion.js
1/4
π
Executing function context...
Tutor: Recursion is when a function calls itself. It's like a mirror reflecting a mirror. To avoid infinite loops, we need a 'Base Case' to stop the execution.
The Anatomy
1. The Base Case: The condition that stops the recursion. Without this, you get a RangeError.
2. The Recursive Step: The part where the function calls itself with a smaller or different input.
function factorial(n) {
if (n === 1) return 1; // Base Case
return n * factorial(n - 1); // Step
}
The Call Stack
Each call is "pushed" onto the stack. When the base case is hit, they are "popped" off one by one.
"Think of it as a stack of plates. You can't wash the bottom plate until you remove all those on top of it."
Consult A.D.A. (AI Debugging Assistant)
I am A.D.A. To understand recursion, one must first understand recursion. What puzzles you about it?