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;
}
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
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.
dang apparently we (I mean me and the OP) gotta read that particular topic again
(and I shouldn’t have skipped the entirety of this thread, however late it is in my place right now…). Thanks Mod