Hello, I’ve just completed the Basic JavaScript portion of the JavaScript curriculum. I completed the few recursion lessons without the hints, but they’re pretty simple, considering they’re nearly identical to the example questions. I’ve watched a few youtube videos, now trying to really understand recursion, but I still don’t feel comfortable with it at all. I get the gist of recursion, it’s a function which calls itself, and you need a base case and recursive case…
But what I don’t get is the intricate workings of what’s happening behind the scenes when the recursive call is ‘stacking’, and then when the basecase is met and returned, how it begins to unpack, or work back through all those stacks. (sorry if that language is confusing, I don’t have the correct terminology yet).
The first piece of code below was written to pass the last lesson of the chapter. Below that is a piece of code I was playing around with to see if I could write it a different way, however, it doesn’t work and I don’t know why. It ends with only 1 being in the array. I have been using PythonTutor to try and step through problems to better understand it, but PythonTutor doesn’t work very well for showing what exactly is going on within recursive functions and how they’re recalled when the basecase is met.
//first piece of code
function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return [];
}
else {
const rangeArray = rangeOfNumbers(startNum + 1, endNum);
rangeArray.unshift(startNum);
return rangeArray
}
};
//second piece of code which doesn’t work and only displays [ 1 ] at end.
function rangeOfNumbers(startNum, endNum) {
const rangeArray = [];
if (startNum > endNum) {
return
}
else {
rangeOfNumbers(startNum + 1, endNum);
rangeArray.unshift(startNum);
return rangeArray
}
};
console.log(rangeOfNumbers(1, 5));
I think the reason I’m not fully understanding recursion is because I’m not grasping what’s going on behind the scenes when the recursive function is called within the function itself… I don’t think I’m comprehending how that data is stored (stacked?) as the recursive functions are called, and what exactly happens when the base case is reached and returned, thereby recalling all those previous stacks… For example, this piece of code:
const rangeArray = rangeOfNumbers(startNum + 1, endNum);
When the basecase is met, why is the ‘const rangeArray’ only filled with the empty array from the basecase return? Why isn’t each rangeOfNumbers(startNum + 1, endNum) which is being recalled at that point being stored in to rangeArray as it’s cycling back through all the stacks?
I’m sorry my language isn’t correct, and confusing for your to follow, just started working through this earlier in the week, so my vocabulary isn’t strong yet. Hopefully you can understand what I was trying to say.
To summarize my questions:
- why doesn’t my second piece of code work, and it only displays [1]
- how exactly does the ‘stacking’ work whenrecursive calls are being made, and, when the basecase is met and return is triggered, how then are they worked back through…
- Why doesn’t the const rangeArray get changed as all the recursive calls are being returned.
Thanks for your help.
Link to the challenge: