Tell us what’s happening:
when I was tackling this challenge, it took me a while to understand the flow of execution and I just want to confirm whats going with my code.
So at first, the function is called , parameters are passed , it steps over first condition because its false. The else part calls the function. At this point the recursive loops start and it keeps repeating it self till startNum reaches the same value as endNum. this is when the loop is broken and an array with startNum=10 is returned. however, startNum that called the function was valued at 9 and so 9 got unshifted to the front of the list. The rest of the function calls before this repeated until 5 was reached.
and numArray was returned.
Can anyone confirm that this is actually what is hapenning with the code?
**Your code so far**
function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum){
return [startNum];
}else {
const numArray = rangeOfNumbers(startNum+1,endNum)
numArray.unshift(startNum)
return numArray
}
};
console.log(rangeOfNumbers(5,10))
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0
Challenge: Use Recursion to Create a Range of Numbers
Link to the challenge: