Recursion!? Help please?

Tell us what’s happening:
Describe your issue in detail here.
Hi, I have done a lot of reading and watching youtube videos on recursion in javascript and I have tried writing recursive functions but I can’t seem to write one originally. I guess my question is precedence. with these functions should the base case come first? should the iteration come second and the return third? or can it be done in any order?

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {
return [];
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

It might be interesting to try it either way, if the order is important it should come out in such tests.

1 Like

HI @kominid33 !

Recursion is a weird topic.
It takes most people some time to wrap their heads around it.
Don’t feel bad about that.

I just tested it and my answer still passed when I put the recursive case first then the base case second.

For me personally, I like to have the base case first and then the recursive case after.

// base case
if (condition) {
} else {
// recursive case

I usually start with the base case since that is the easiest part :slight_smile: How would you call this function to trigger just the base case? For example:


What makes this the base case? What do you want it to return for the base case? Remember, the function must always return the same type of value. In this case it is an array of at least one number.

After you have the base case down then think about calling the function with only one step before the base case:


Since you are one step out from the base case you know you are going to have to call the function recursively to trigger the base case, and you already know how to do this. The trick here is knowing how to use the result of the recursive base case return value to return the proper value in the original function call. Remember, you always have to return the same type of value.

Also, don’t let my use of actual numbers in the examples above throw you off. Inside of the function you will not use actual numbers, you will use the variables available to you.

1 Like

Hi again, TY all for replies. I think it is the value type return that is my problem. the function uses integers for the parameters but the challenge asks for an array. I should know this at this stage, could someone point me in the right direction.

I would suggest tackling the base case first and then should help you see where the array is coming from.

Here is the base case
It should also work for cases where both startNum and endNum are the same.
For example,
rangeOfNumbers(4, 4) should return [4] .

Once you get the base case, then you can start to tackle the recursive case.

1 Like

Hi @kominid33 , you need to read aboout :

  • call stack
  • and nested calls

I tried to explain (how this exercise is solved using those abstractions) in another thread (is an explanation of the solution):

Cheers and happy coding :slight_smile:

1 Like

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