Basic JavaScript - Use Recursion to Create a Range of Numbers

Hi, i wanted to ask about the solution.

function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum) {
    return [];
  } else {
    const numbers = rangeOfNumbers(startNum, endNum - 1);
    return numbers;

In order to push endNum into the numbers array you need to create that array right? But you didn’t. You created a numbers variable, this is what I don’t understand, maybe I don’t get the topic , would you please explain this in some way? :slightly_smiling_face:

The function rangeOfNumbers() always returns an array.

Hmm…Why is that? Is there any rule?

It is explicitly what the function is designed to do.

Also, the base case returns an array, and the recursive case returns an array.

1 Like