Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Could you explain me this freecodecamp recursion function, please?

Here’s the code

function rangeOfNumbers(startNum, endNum) {
  return startNum === endNum
    ? [startNum]
    : rangeOfNumbers(startNum, endNum - 1).concat(endNum);
}

I understand that until startNum equals endNum it will recall itself, but the thing that I don’t understand is where the value is stored?

Say for example it’s rangeOfNumbers(3,6)
So it’s gonna be like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

6-1
5-1
4-1

Right? And each time the numbers are added to the array and we get [3,4,5,6], but I don’t understand how and where it stores this array.

If I’m not mistaken, concat merges two or more arrays, but there are no arrays.

I just want to have a full understanding of it. Otherwise, I won’t remember it and won’t be able to use it.

>Solution :

When a block (something that starts with { and contains statements) is entered into, a new "variable environment" is created. You could think of this as something that maps each identifier for that block execution to its value.

Each time a function is called, a new such environment is created.

In this case, the parameters startNum and endNum are stored in an environment the first time the function is called. Then, when the interpreter runs across

rangeOfNumbers(startNum, endNum - 1).concat(endNum);

The currently running function (the one linked to the environment just described) gets suspended, and a new function is put onto the call stack, creating another environment. The process repeats itself until the end of the recursive logic is reached and [startNum] is returned (or the stack is blown). At that point, you have a bunch of rangeOfNumbers functions in progress, each with their own environments. At that point, you could imagine it as something like

rangeOfNumbers { startNum: 3, endNum: 5 } (this is the intial call; currently suspended)
  rangeOfNumbers { startNum: 4, endNum: 5 } (currently suspended)
    rangeOfNumbers { startNum: 5, endNum: 5 } (executing, about to return)

The innermost function returns its [startNum] and terminates, and so the last function resumes, now with the useable return value:

: rangeOfNumbers(startNum, endNum - 1).concat(endNum);

gets evaluated to, when the endNum is 5:

: [5].concat(endNum);

The process continues up the stack until all of the recursive calls are finished, and you just have the initial

rangeOfNumbers { startNum: 3, endNum: 5 }

which then finishes itself.

So, while the recursive calls are going on, the values from the prior calls are stored in each of those calls’ environments.

If I’m not mistaken, concat merges two or more arrays, but there are no arrays.

[startNum] is an array returned at the innermost point of recursion. concat can also create a new array by taking one array as an argument, and the value to append as another. For example, [5].concat(4) evaluates to [5, 4].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading