Help with Recursive Arrays

Tell us what’s happening:

I get how to make the code work. I understand what’s needed to complete it but I cannot visualize how it works in this instance.

Where are the variables storing and how are they storing in the correct order? Is there a simulation that goes through each iteration to make it easier to see where the data goes for each step of the code?

In my mind the example answer for this problem will always return 1. How is it creating the array, I don’t see it ever reaching that part of the function. Or is it that it constantly calls itself in the latter half and never returns to the initial if statement? Also if that is the case at what point does it push the answers to the array? After its reached the end of the calling itself for the last time does it return to add each value back to the array?

Your code so far


function rangeOfNumbers(startNum, endNum) {
//return [];
if (startNum === endNum) {
 return [startNum]
} else {
    var numbers = rangeOfNumbers(startNum, endNum - 1);
    numbers.push(endNum);
    return numbers;
}
};

EDIT: I understand recursion, I get that its stacking and that’s how it works. My issue is, in this function, where is the stacking occurring. Am I right in assuming this function recoils? Meaning it runs through creating the numbers until it gets to startNum and then it back tracks adding each to the array.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36.

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-range-of-numbers

Hello and Welcome!

To understand an algorithm (or what a code does), I write on a paper what each iteration of the code does.

In this case, I would write something like this (this will be pseudocode):

// Assuming the function gets called with the params 1, 3: rangeOfNumbers(1, 3)
// Here starts the first call
startNum = 1
endNum = 3

is 1 equal to 3? No, hence the return is skipped.

// Here the function is paused as rangeOfNumbers is called again
numbers = rangeOfNumbers(1, 3 - 1) 
# STEP-1 // Remember this

// Here starts the second call
startNum = 1
endNum = 2

is 1 equal to 2? No, hence the return is skipped again

// Define the numbers variable again, and pause the execution
numbers = rangeOfNumbers(1, 2 - 1) // Go to third call (below)
# STEP-2

// Here starts the third call
startNum = 1
endNum = 1

is 1 equal to 1? Yes, hence we return:
  return [1] // An array with the element 1.
  // Here we continue with the execution of STEP-2

# STEP-2 Continue
// Numbers now equals to: [ 1 ]
[1].push(2) // endNum equals 2, remember
return [ 1, 2 ]; // Here we continue with step 1

# STEP-1 Continue
// Numbers now equals to: [ 1, 2 ]
[ 1, 2 ].push(3) // endNum equals 3, remember?
return [ 1, 2, 3 ] // This finishes the execution

This syntax seems weird, but the idea is to understand what’s going on with each call to the function.

Basically, the function gets called recursively until the if is true, then it starts to fill the array with each return.

I hope it helps, if not, ask again :stuck_out_tongue:.

When a function contains a recursive call, it doesn’t end until that call completes. A different instance of the function is doing each recursive call. The var numbers... line just waits for a value to be returned by the new instance. Each instance of the function has its own member variables that exist as long as that function is running.

for a visualisation try executing your code with this: http://pythontutor.com/javascript.html#mode=edit

4 Likes