**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 ]

Link to the challenge:

Works for me. What browser are you using?

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

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
2 Likes

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

that’s similar to my own solution, and this works fine on my laptop:

function rangeOfNumbers(startNum, endNum) {
  if (startNum > endNum) {
    return [];
  }
  else {
    const numArray = rangeOfNumbers(startNum+1, endNum);
    numArray.unshift(startNum);
    return numArray;
  }
};

console.log(rangeOfNumbers(5, 10))

the last line above is just to check my answer

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.

then I suggest to swap the order of these lines:

  myArray.push(startNum);
  rangeOfNumbers(startNum + 1, endNum);

or maybe it’s something to do with the

return myArray;

which is in the function block rather than the if-else block

The issue with the original code is the use of global variables.

1 Like

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