Recursion in javascript is eluding me. I'm almost there, but I'm a bit confused about the order of operations. Take for example the following function:
function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) {
return [startNum];
} else {
var numbers = rangeOfNumbers(startNum, endNum - 1);
numbers.push(endNum);
return numbers;
}
}
Questions I'm trying to figure out:
Does this create a closure around
numbers?Does the function
rangeOfNumbersreturnnumbersevery single time, or only once the recursion is completely finished?For the base case: Why are we returning an array with only
startNumas the "base condition" ? Wouldn't this overwrite thenumbersvariable upon returning? I'm not sure how the base case works exactly. It seems to always return something that isn't what we are looking for, yet it controls when the function should stop executing (at the first start of the function call, and/or for ending it).