Basic JS last Question

Can someone explain how this recursion works?
If i understand correctly if startNum is equal to endNum then if its true then return ? array [startNum or endNum] it is equal so which ever floats the boat.
else if false : then call itself. Now this is the confusing bit when it is calling itself is it counting down or - 1 from endNum or from both? Another confusing bit if its - 1 from endNum does that mean the first input is already - 1 ( -1 below endNum)that is why we need to concat(endNum)?

  **Your code so far**

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

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

You concat to form the range of numbers.

For example, let’s say we put (1, 5) into the function.
It checks if they are equal, they are not obviously, what it does is it forms an array which is [rangeOfNumbers(1, 4), 5]. Concatination at the end what forms the array.

Then for the rangeOfNumbers(1, 4), it forms another array which is [rangeOfNumbers(1, 3), 4, 5].

It goes like this until they are equal. Once they are equal it adds the number 1 as the first element of the array.

The step just before the result looks like this: [rangeOfNumbers(1, 1), 2, 3, 4, 5]

1 Like

The easiest way to see what is going on here is to just map it out on paper with a simple example. Use rangeOfNumbers(1, 3). You will hit the else and return:

a) return rangeOfNumbers(1, 2).concat(3)

But we can’t really return a value yet because we have to wait for the recursive call to return a value (just like we would have to wait for any other function to return a value).

Now we execute the recursive call in a), which will hit the else as well and return:

b) return rangeOfNumbers(1, 1).concat(2)

Again, we can’t actually return a value here because we have to wait for the recursive call in b) to return a value. Do you see a pattern here? We spend a lot of time waiting around for functions to return a value.

Now we execute the recursive call in b) and this time it hits the if and returns:

c) return [1]

Hey, we hit the base case and can actually return a value! So c) is the return value for the recursive call in b), and you can replace the recursive call in b) with this answer and now b) can be written as:

b) [1].concat(2) which is [1, 2]

Do you see what just happened. The recursive call in b) was just waiting for its return value. As soon as it gets the return value then it can complete the return.

Just like b) was waiting for a return value, a) is also waiting for a return value. We now have that return value in our updated b) and so now we can replace the recursive call in a) with the return value in b):

a) [1, 2].concat(3) which is [1, 2, 3] (our final answer)

Ta da! Recursion is basically a pattern of continuously making recursive function calls and waiting for those calls to return a value. We only return a value when we hit the base case and then we can start working our way backwards, filling in all those values we were waiting for.

its a good link for understanding recursion. i hope this helps…

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