Hi, I solved this recursion problem but I am specifically confused about HOW the values end up inside the array. I understand that the functions ‘stack’ until they hit the base case. But when the function returns the [endNum] what is happening with the ‘stacked’ function calls that push the endNum’s to the array? How does using push on the const ‘list’ result in numbers in the array?
Thanks for any help!
Your code so far
function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
return [endNum];
} else {
const list = rangeOfNumbers(startNum, endNum - 1);
list.push(endNum);
return list;
}
};
console.log(rangeOfNumbers(1, 7));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36.
Challenge: Use Recursion to Create a Range of Numbers
Link to the challenge: