I dont understand why my code is returning all of the numbers except for startNum.
THE TASK 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. It should also work for cases where both startNum and endNum are the same.
function rangeOfNumbers(startNum, endNum) {
if (startNum >= endNum) {
return [];
}
let newArr = rangeOfNumbers(startNum, endNum - 1);
newArr.push(endNum);
return newArr;
};
console.log(rangeOfNumbers(17, 33));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.56
Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers
OK so I noticed that startNum can be equaled to endNum. The challenge now passes all tests after making that change. But Im not understanding how making that change now allows startNum to be included in the range all of a sudden.
Think of the number of times the recursive function is called the same way you think about how many times a loop runs. What is the state of the program before the base case and has it reached the expected state (state as in all the variables)?
What happens if the function is allowed to be called one extra time?
Why is endNum not decremented as many times as it needs to be?
When does it get decremented, does the function have to be called for it to be decremented?