Confuse about recursion value

Tell us what’s happening:
Describe your issue in detail here.
I dont understand this line of code mean:
let num = rangeOfNumbers(startNum, endNum - 1);

What is num value now? is it a function? for example endNum is 3 and startNum is 0 now num is rangeOfNumbers(0, 2);?

if yes what does that mean? a variable that become a function i am confuse about that.

  **Your code so far**

function rangeOfNumbers(startNum, endNum)
if (endNum - startNum === 0)
    return [startNum];
   let num = rangeOfNumbers(startNum, endNum - 1);
   return num;


  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

This line of code

let num = rangeOfNumbers(startNum, endNum - 1);

assigns the value returned from rangeOfNumbers(startNum, endNum - 1) to the variable num.

So after that statement executes, the variable num contains whatever has been returned from the rangeOfNumbers function.

a little more clear thank you. But i unclear on:
assigns the value returned from *rangeOfNumbers(startNum, endNum - 1)* to the variable num.

for example: startNum = 0 ; endNum = 2;
now become let num = rangeOfNumbers(0, 1);
how a rangeOfNumbers(0, 1); can use push *num.push(endNum);* ?

Because as it works through that function, it first gets to rangeOfNumbers and has to stop at that line to evaluate that recursive call, so it starts a function call, then it gets to the line with rangeOfNumbers, has to evaluate that recursive call, so it starts a function call, then it gets to the line with rangeOfNumbers… it keeps doing that until it has a function call that can finish, when endNum - startNum === 0, then it can finally finish a function call and it returns an array, after that, they incomplete function calls cascade finish, returning the array each time, so that push line will always have an array.

I think a big part of understanding recursion is understanding how the call stack works, even in just a qualitative sense. Put in a bunch of log statements and see in what order different lines get called. Or search the forum - there are A LOT of threads discussing this, some with detailed explanations.

And remember that recursion is a confusing subject, so don’t get frustrated. You shouldn’t expect to understand it right away.

From the instructions

The function should return an array of integers

So, the rangeOfNumbers(startNum, endNum - 1) function returns an array.
In the code that you posted, that array is stored in the variable num.

And, as push() is an array method, it can be used to modify the array in the num variable.

Personally, I’d choose a different name for that variable - newArray perhaps? - as I think the variable name might be confusing you.

In this forum post, @lasjorg posted a link to a Visualizer that might help you understand what’s happening in the recursion.

thank you i will see some more post from thread.

Yeah maybe that also confusing me too.Thanks for pointing that out,will check that forum too.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.