Java script recursion confusion.
I don’t understand why pushing a value that is gradually decreasing by one would lead to a rising array…
function rangeOfNumbers(startNum, endNum) {
if (endNum<startNum) {
return ;
}else{
const myArray = rangeOfNumbers (startNum, endNum-1);
myArray.push(endNum);
return myArray;
}
}
I would have thought this code would create the array 5,4,3,2,1 and yet apparently this actually leads to the array 1,2,3,4,5.
What am I not understanding?
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.46
Basic JavaScript: Use Recursion to Create a Range of Numbers | freeCodeCamp.org