# Turn recursion into for loop

Tell us what’s happening:

I already completed the assignment but i wanted to know how would I go about converting this assignment into a for loop? I am just curious?

``````
function rangeOfNumbers(startNum, endNum) {
return [];
};

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36`.

Challenge: Use Recursion to Create a Range of Numbers

Hello~!

What do you think the approach would be? What does the recursive function do? Do you understand the recursion?

Walking through the logic step-by-step should help you determine how you’d use a `for` loop.

I know the recursion will call itself until its base case turns false, then the stack will unwind and be pushed into the array. I was thinking about nesting an for loop inside a if statement but this is where I get confused. @nhcarrigan

So what is the output of the function? What would `rangeOfNumbers(1, 5)` give us? How about `rangeOfNumbers(24, 29)`?

You do not need to do this. The function can be written with a variable and the `for` loop.

1 Like

@nhcarrigan The output of one and five would be [1,2,3,4,5] and 24, and 29 would be [24,25,26,27,28,29] right if your using the unshift method. So, could I decrement the loop ?

You could decrement the loop, yes. Or you could use the `.push()` method to add to the end of the array and increment the loop. Either approach could work.

1 Like

@nhcarrigan Got it, I will try it out thanks for the help!

1 Like

For anyone wanting to know the output
function rangeOfNumbers(startNum, endNum) {

``````    function rangeOfNumbers(startNum, endNum) {
let numbers = [];
for(let i=endNum; i>=startNum; i--){
numbers.unshift(i);
}
return numbers;

};
console.log(rangeOfNumbers(1,5));``````