'How to use recursion to create a range of numbers?
Here is the problem.
Use Recursion to Create a Range of Numbers Continuing from the previous challenge, we provide you another opportunity to create a recursive function to solve a problem.
We have defined a function named rangeOfNumbers with two parameters. The function should return an array of integers which begins with a number represented by the startNum parameter and ends with a number represented by the endNum parameter. The starting number will always be less than or equal to the ending number. Your function must use recursion by calling itself and not use loops of any kind. It should also work for cases where both startNum and endNum are the same.
my solution was
function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0);
return [startNum];
} else {
const numbers = rangeOfNumbers(startNum, endNum - 1);
numbers.push(endNum);
return numbers;
}
}
Why is this wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
