Stuck on Use Recursion to Create a Range of Numbers

@Sboonny sorry I mixed up my array names. I’m again trying to make startNum and endNum reach the base case. I thought if I made startNum bigger and endNum smaller each time recursion happened that would work. But it doesn’t.

I tried giving it two arguments but I still don’t know what arguments I need to make it work.

is startNum ever going to be bigger?

…isn’t that the point of the recursion? Making startNum bigger to reach the base case of the else? Otherwise I don’t know how recursion works at all.

function rangeOfNumbers(startNum, endNum) {
if (startNum <= endNum){
const arr = rangeOfNumbers(startNum + 1 && endNum - 1);
console.log(arr);
return arr;
} else {
return [];
}
};

Hi, I’m afraid I really struggle to focus on videos and they really don’t work for me to explain coding. I need to either be reading something, or doing it to learn.

Okay I actually don’t understand the previous lesson. I solved it but it makes zero sense why it works. How does n go from -1 to a number over 1 by putting n which is -1 to the front of the array. How does adding two -1 make a positive number?

Recursion seems an over complex solution to simple problems. I really don’t know why it’s better than a for loop when it’s so unrelentingly confusing and hard to implement.

You are correct, you would never use recursion to solve this type of problem in the real world. The lessons here are just trying to introduce you to the concept with very simple examples. There are some problems in which recursion does actually make the solution much easier though. But you probably won’t run into those doing general front end web development.

But if you can understand these simple recursions then you will have a better understanding of how functions work in general and also how recursion works.

2 Likes

Formatting is helpful for both you and to people who you are asking to help you:

function rangeOfNumbers(startNum, endNum) {
  if (startNum <= endNum) {
    const arr = rangeOfNumbers(startNum + 1 && endNum - 1);
    console.log(arr);
    return arr;
  } else {
    return [];
  }
};

I don’t really understand the arguments for your recursive function call.


Every recursive function has the same basic structure

function myRecursiveFunction(arguments) {
 if (baseCase) {
    return someSimpleValue;
 } else {
    const intermediateResult = myRecursiveFunction(modifiedArguments);
    return modifiedIntermediateValue;
 }
}

The base case is the absolute simplest version of the problem. In this challenge, the absolute simplest version of the problem would be a range with no numbers in it.

The recursive case is based upon a simpler version of the problem. Making an array with all values from 7 to 42 is very easy is someone hands you an array with all values from 7 to 41, and you have a function that can make an array with all values from 7 to 41.


There are some problems where recursion is the easiest way to express the logic. This isn’t one of them. This is more of a toy problem to practice the idea of recursion. But it’s hard because 1) recursion is hard and 2) it requires a strong understanding of function calls, function scope, and return values.

1 Like

Hi, I’m afraid I really struggle to focus on videos and they really don’t work for me to explain coding. I need to either be reading something, or doing it to learn.

No worries, here is old post about the previous step explain recursion in details.

and if you feel it’s spoiler, here is a news article about it

What is Recursion? A Recursive Function Explained with JavaScript Code Examples (freecodecamp.org)

1 Like

Okay I’ll look into standard practice for Javascript formatting, probably isn’t helping me either if my code is a mess.

I don’t understand them honestly. I’m just throwing anything at the wall incase something sticks. :dotted_line_face:

Oh. Okay that explains a lot. I didn’t realise the base case was a range with no numbers. I thought it was if startNum was bigger than endNum. :sweat_smile:

If you right click in the editor, you should get an option to format code automatically.

The start being after the stop is a formal way to check that the range is empty. No sequence can start at 11 and stop at 10.

1 Like

Okay, I have no idea what goes into modifiedArguments.

I had an idea but it didn’t work. So if I have 3 and 5, I need to get 3 to equal 5 to reach the base case. So I could divide 5 by 3, turn 2.5 into a whole number then add it to 3 to get 5.

But that didn’t work. I am really lost how to reach the base case. Nothing works.

Why so complicated? Have you tried addition instead?

How did I make the recursive case here?

Adding startNum and endNum doesn’t work. I’ve tried it.

I don’t know. I’m sorry I honestly don’t know how that is a recursive case or what a recursive case is.

Don’t add them. Add a value to 3.

Ignore the terminology for a second. I said that making an array of values from 7 to 42 is easy if you have an array of values from 7 to 41. How are those to arrays related?

One is one number smaller than the other?

You’d have to add one to get an array from 41 to 42.

But if I add a number to startNum then it won’t work for every case.

And I can’t add half of endNum to it because I have no idea how to write that in Javascript.

Yep, that’s exactly right

There is no zero.

Try a smaller example.

We want a list of numbers from 2 to 5.

That would be easy if we had a list from 2 to 4.

That would be easy if we had a list from 2 to 3.

That would be easy if we had a list from 2 to 2.

That would be easy if we had a list from 2 to 1.

That can’t exist, so the list from 2 to 1 is [].

But then we can use that to create a list from 2 to 2: [2]

But then we can use that to create a list from 2 to 3: [2, 3]

But then we can use that to create a list from 2 to 4: [2, 3, 4]

But then we can use that to create a list from 2 to 5: [2, 3, 4, 5]

I can’t get it to work. I don’t know how to do it.

function rangeOfNumbers(startNum, endNum) {
if (startNum == endNum){
return [];
}
else {
  const arrNum = rangeOfNumbers (startNum + (endNum / 2));
  return arrNum;
}
};