Use Recursion to Create a Range of Numbers

Use Recursion to Create a Range of Numbers
0

Please someone explain how the else part work.

I know the rule of Recursive Function, that calls itself during its execution.

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0) {
    return [startNum];
  }  else {
   var numbers = rangeOfNumbers(startNum, endNum - 1);
   numbers.push(endNum);
   return numbers;
  }
}

Please use /spoiler to blur the solution for other users.

But anyways, much like how recursion was used to create a list of consecutive numbers in the example, we call the function itself. Once it gets to the base value, where the starting and ending number are the same, it has already pushed all the previous values into the list. Then, finally it executes a return to break out.

1 Like

I used the following and it worked as well.

<redacted>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.