Sorry for the late reply, but here’s the rundown of what your code is doing.
First I suggest you check out this article on recursion just to understand the concept:
Basically your setting a parameter with your first If statement. This is the recursive call that in your else statement is going to keep executing until startnum === 0 .
Take a look at my comments on your code:
function rangeOfNumbers(startNum, endNum) {
if(endNum - startNum === 0){
return [startNum]; // If there is no endNum return the first number
}else {
// Works like a loop to continually push the numbers from startNum to endNum
var arr = rangeOfNumbers(startNum,endNum - 1); // Starts at startNum but subtracts 1- from endNum
arr.push(endNum); //pushes the number each time recursive function is called
return arr; // return the final array
}
};
rangeOfNumbers(1,5);
Here’s how the flow of recursion works. You keep calling the same function with different values fo endNum. At the end of recursive calls, the end case (startNum === endNum) becomes true and recursion stops. The answer is passed back to the caller, building the answer as you keep returning. In the diagram, I used the term ‘Concat’ but it’s a push method, arr.push(endNum).