# **Challenge:** Use Recursion to Create a Range of Numbers

Tell us what’s happening:
I’m working on the “Use Recursion to Create a Range of Numbers”. When I run the test cases in the console, my code appears to work, but when I submit, I’m getting an failed result for the following cases:

• `rangeOfNumbers(1, 5)` should return `[1, 2, 3, 4, 5]` .
• `rangeOfNumbers(6, 9)` should return `[6, 7, 8, 9]` .
• `rangeOfNumbers(4, 4)` should return `[4]` .
'Any thoughts on why this is code isn’t working?
``````let myArray = []
function rangeOfNumbers(startNum, endNum) {
//Base case
if (startNum > endNum) {
return[];
//recursive call
} else {
myArray.push(startNum);
rangeOfNumbers(startNum + 1, endNum);
}
return myArray;
}
``````

console.log(rangeOfNumbers(1, 5));
// returns [ 1, 2, 3, 4, 5 ]

console.log(rangeOfNumbers(6, 9));
//returns [ 6, 7, 8, 9 ]

console.log(rangeOfNumbers(4, 4));
//returns [ 4 ]

Works for me. What browser are you using?

using safari… I reset the lesson and pasted in the code and it worked fine

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````
1 Like

A post was split to a new topic: Confusion in Recursion