Instructions

Write a function called numberRange that takes in a startNum and an endNum and returns an array of numbers from startNum to endNum, inclusive. Be sure to use recursion in your solution.

Function Signature

/**
 * Returns an array of numbers from startNum to endNum, inclusive.
 * @param {number} startNum - The starting number.
 * @param {number} endNum - The ending number.
 * @returns {number[]} - An array of numbers.
 */
function numberRange(startNum: number, endNum: number): number[];

Examples

numberRange(1, 5); // should return [1, 2, 3, 4, 5]
numberRange(3, 10); // should return [3, 4, 5, 6, 7, 8, 9, 10]
numberRange(7, 7); // should return [7] (only one number)

Hints

Explanation

That is the gist of it, but let's go step by step for numberRange(1, 5)

Step 1: Since startNum(1) is not equal to endNum(5), we move to the recursive block.

Step 2: We make a recursive call to numberRange(1, 4), which will further call numberRange(1, 3) and so on, until we reach the base case.

Step 3: The base case is reached when startNum(1) becomes equal to endNum (1). At this point, the function returns an array containing just the number 1.

Step 4: The unwinding begins. The result of numberRange(1, 2) is now an array [1, 2]. Then, the result of numberRange(1, 3) is [1, 2, 3], and so on, until we finally get the result of numberRange(1, 5) as [1, 2, 3, 4, 5].

The function follows the same recursive process to build the array of numbers, starting with the base case and adding numbers one by one as it unwinds the recursive calls.

</details>