So, here’s my solution to the problem:
function rangeOfNumbers(startNum, endNum) {
if (startNum > endNum) {
return [];
}
else {
const countArray = rangeOfNumbers (startNum + 1, endNum);
countArray.unshift(startNum);
return countArray;
}
};
My solution is different from the solutions provided but it passes and is all fine, and I nearly understand why it works, even though recursive functions have taken me a while to understand.
There is one line of the code I don’t understand:
const countArray = rangeOfNumbers (startNum + 1, endNum);
Why does our variable need to be assigned to the function with different values?
Is this the line of code that calls the function again with different values? If so, why do we need to assign to a variable?
Hope someone can help me with that, still new to JavaScript so I apologize if it’s a dumb question.
Thank you