Use Recursion to Create a Range of Numbers1

Hi, could someone explain me this task. I don’t understand the description and task, which meant to become a code.

  **Your code so far**

function rangeOfNumbers(startNum, endNum) {

if (startNum <= endNum) {
return [];
} else {
return arr = startNum * (startNum - 1) + 1;

}
};

I see that condition is written incorrectly, because it will run endless, or it won’t add numbers, just an empty array.

  **Your browser information:**

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

Challenge: Use Recursion to Create a Range of Numbers

Link to the challenge:

You want to return an array of numbers, the first element in the array is the startNum passed into the function and then each number after that is one bigger than the previous number until you reach endNum. You can look at the examples below the instructions:

rangeOfNumbers(6, 9) should return [6, 7, 8, 9]

You can easily do this with a simple for loop but in this case you need to use recursion.

This post was flagged by the community and is temporarily hidden.

Hi! thanks for that, but I still can’t pass. I am having trouble to understand it.

I wrote this using the loop:

for (let arr = 0; arr <= startNum; i++) {

if (startNum <= endNum) {

  return arr.push[startNum, endNum];

} else {

  endNum - startNum === 0 {

    return arr;

  }

I try to make this in recursion:

function rangeOfNumbers(startNum, endNum) {

 

  if (startNum <= endNum) {

  return arr.push[startNum];

  } else {

return arr = [endNum, (startNum, endNum - 1) + 1];

  }

Could you point what I am doing incorrectly?

Your recursive version has several problems, for example:

Where is the variable arr defined in your function? Hint, it is not, so you can’t push an item onto an array that isn’t defined.

Also, remember, in a recursive function you have to call the function inside of itself. So your recursive function should include a call to rangeOfNumbers somewhere.

I’d start with the base case (the if statement). The base case doesn’t make a recursive call, it just returns a value. It is the end of the line, it stops the recursive calls from happening. In what scenario would the base case be triggered? In other words, if you are calling your function:

rangeOfNumbers(start, end)

What are possible values for start and end that would trigger the base case. What numbers would cause rangeOfNumbers to only run once? In other words, the first time it is called it would trigger the if statement and just return a value without making a recursive call. Try creating a new if block that correctly triggers the base case and returns the correct value for the base case.

1 Like
function rangeOfNumbers(startNum, endNum) {

  if (startNum === endNum) {   // my base case says startNum strictly equals endNum, that triggers the code & returns startNum in an array and prevents an infinite.

  return [startNum];

  } else {      // if it doesn't equal then calls the function itself and assign to var arr. Also, it will decrement endNum, because of taking the next number

    var arr = rangeOfNumbers(startNum, endNum - 1); // so, we will see the same startNum, but endNum will be less

    arr.push(endNum);  //then the result of the self call will be added at the end of the arr variable

    return arr; 

  };

};

When the endNum equals startNum it will trigger the base case and stop the recursion.

console.log(rangeOfNumbers(1, 5));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you, I will learn to use it.

I have a question regarding the below code, which works alright.

If I swap for decrementing endNum, this will print again the same number, why is that?

var arr = rangeOfNumbers(startNum, endNum - 1);

Look at the line after this one:

arr.push(endNum);

Do you see why using the decrement operator on endNum might cause a problem with this line?

Not really… I learnt that endNum - 1 = --endNum.
However, the code didn’t work. I have been thinking that the decrement operator makes a result, which is pushed to the arr.
In the case of the recursion the - 1, just put a result of endNum - 1 in the stock and passing to the argument and that (initial) endNum is just pushed to the arr. Until the base case is true.

Yes, they return the same value but the decrement operator has a side effect in that it actually reduces the value of the variable by one.

let num = 5;
console.log(num - 1); // prints 4 but num still equals 5
console.log(--num); // prints 4 but num now equals 4

Do you see how the decrement operator is going to affect the line

arr.push(endNum);

Yes:
this console.log(num - 1); will push 5
this console.log(--num ); will push 4