Stuck on Use Recursion to Create a Range of Numbers

Okay so this one is really confusing me. I think I’ve got the first if right but no idea what to do for the else to get this working. I need some way to put the values between startNum and endNum into the array but have no idea how.

function rangeOfNumbers(startNum, endNum) {
if (startNum <= endNum){
return [];
} else {
  const arrNum = (endNum - startNum);
  console.log(arrNum);
}
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Firefox/102.0

Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers

Link to the challenge:

Did you solve the previous countdown challenge?

1 Like

Yes, but I’m not totally confident I know why my code worked.

If you understand that, this one is easier, as that was from 0 to n, this is a range from startNum to endNum

I’m honestly finding it far harder than the previous lesson. I keep getting an empty array and I don’t know how to make it not empty.


function rangeOfNumbers(startNum, endNum) {
  if (startNum <= endNum){
  return [];
  } else {
    const arrNum = (endNum - startNum);
    arrNum.push(n);
    console.log(arrNum);
    return arrNum;
  }
};
console.log(rangeOfNumbers(1, 5));

Okay I know why this code is wrong now, but I have no idea what the right code is.

Your condition says if startNum <= endNum return [] so that’s what it’s doing.
Also, arrNum.push(n) - what exactly is n here? It is not defined.
There are 4 slight changes you need to make to make it work.

1 Like

One of the big things about recursion is that it need to call itself. Is your function calling itself anywhere in your function?

1 Like

…I did not notice the n that was plonked in the middle of my code. But that explains a lot about what may have been going wrong. :sweat_smile:

Okay I fixed the n part and also.

Oh. Oh. I’ve got my logic the wrong way around.

@Montin @Lego_My_Eggo

I’ve tried reworking my logic but I’m still stuck here. I can’t get it to both return an array and call the function.

function rangeOfNumbers(startNum, endNum) {
if (startNum >= endNum){
  return [];
}
else {
const arrNum = rangeOfNumbers(startNum,endNum);
arrNum.push(startNum);
console.log(arrNum);
return arrNum;
}
};

An important thing in recursion is to change the arguments you use to call the function

You are not doing it

3 Likes

You can’t pass in the same numbers each time or the recursion will never end. One of those values needs to change so you can gradually reach the base case.

2 Likes

What does that mean I don’t understand. It is something to do with the if/else chain?

But aren’t I supposed to use these values? If I’m not using startNum and endNum what am I supposed to put?

This webpage explained the recursion needs to happen until the base case, the if is reached. but doesn’t subtracting startNum from endNum do that?

And why does this not return an array but the structure of code from the previous challenge return an array?

function rangeOfNumbers(startNum, endNum) {
if (startNum >= endNum){
  return [];
}
else {
  const numArr = rangeOfNumbers(startNum - endNum);
  return countArray;
}
};

You need to use startNum and endNum, but they will need to change to reach the base case. If you call rangeOfNumbers(1, 5) its looking at your if statement and saying false, then it goes to const numArr = rangeOfNumbers(startNum - endNum) which would be rangeOfNumbers(-4). you are then using the if( startNum >= endNum) with startNum = -4 and endNum = undefined. Is that ever going to work? or do you need to modify endNum or startNum in some other way to reach your base case?

1 Like

I don’t know I’m sorry I am still complete lost.

I tried adding startNum and endNum but this is because I’m just throwing random things at the wall to try get it to work. I am really confused.

function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum){
  const numArr = rangeOfNumbers(endNum + startNum);
return countArray;
} else {
return [];
}
};
rangeOfNumbers(1, 5)
if (startNum > endNum) {
  const numArr = rangeOfNumbers(endNum + startNum);
return countArray;
}

Just wondering, what is countArray, I don’t see it somewhere else in the function

Recursion in Programming - Full Course - YouTube

Edit: @EllaGriff btw, I can understand how you feel. Although this is basic JavaScript, it isn’t basic level.

is startNum ever going to be bigger?

rangeOfNumbers expect two arguments, here you are giving it only one

what is this? you never defined it


if you understand the countdown/countup functions of the previous challenge, this is going to be much easier

1 Like