Tell us what’s happening:
Describe your issue in detail here.
SPOILER ALERT…THE CODE BELOW WORKS SO DON’T READ AHEAD IF STILL TRYING TO SOLVE…I AM ASKING FOR CLARIFICATION . THANKS.
I understand the concept of recursion and how it calls itself until reaching the base case. If someone can clarify for my though…Why does the “else” portion of the function not perform the numbers.push(Num) until after everything has been called? I don’t understand why it goes back to the origianl call with the endNum - 1 rather than just take that endNum straight through the “else” section.
Thanks in advance for any help trying to explain this;
Your code so far
<SPOILER - this code works so DO NOT READ if you are still working on this problem>\
I am simply asking for clarification. Please correct me if I am wrong doing it this way. I am still not 100% sure of the ethics of posting on the forum.
function rangeOfNumbers (startNum, endNum) {
if (endNum < startNum) {
return [];
} else {
const numbers = rangeOfNumbers(startNum, endNum - 1)
numbers.push(endNum);
return numbers;
}
};
console.log(rangeOfNumbers(1,5));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers
Link to the challenge: