Hi people, once again I’m thinking about details that bug me.
I’m doing this last excersise and I’m thinking “how come the 4 goes into the array at the end?” the push instruction shouldn’t be executed if the difference between endNum and startNum is equal to 0, arrayOfNumbers isn’t inside the if, and if I change the return statement of the if, I get that value inside the array, so obviously the base case manages to put that number inside the array.
Another thing, I’m still struggling to understand why the final array shows the numbers in ascendent order if there’s a push. I didn’t understand the reason in the previous lesson, I need a dumbed down version, thanks again!
function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum == 0) {
return [startNum];
} else {
let arrayOfNumbers = rangeOfNumbers(startNum, endNum -1) ;
arrayOfNumbers.push(endNum);
return arrayOfNumbers;
}
};
console.log(rangeOfNumbers(4, 4));
console.log(rangeOfNumbers(4, 10));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36
Challenge: Use Recursion to Create a Range of Numbers
Link to the challenge: