Tell us what’s happening:
Describe your issue in detail here.
I’d like to have an explanation about the direction of push and unshift for recursion values of +1 increment and -1 increment.
if I use push for endNum -1, I get array of [ 6, 7, 8, 9 ] for (6, 9).
if it’s unshift, it’s array of [ 9, 8, 7, 6 ].
On the other hand if I use startNum + 1:
If I use push for startNum +1, I get an array of [ 9, 8, 7, 6 ].
If I use unshift for startNum +1, I get array of [ 6, 7, 8, 9 ]
please explain the pattern of why it is so?
**Your code so far**
let newArray = [];
function rangeOfNumbers(startNum, endNum) {
if (startNum === endNum) {
return [startNum];
}
else if (startNum < endNum) {
const newArray = rangeOfNumbers(startNum, endNum -1);
newArray.push(endNum);
return newArray;
}
};
console.log(rangeOfNumbers(6, 9));
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Use Recursion to Create a Range of Numbers
Link to the challenge: