Using Recursion to Create a Range


I tried to get past this challenge but I can’t and I don’t know where is the problem. My code does what it is supposed to do but when I run the tests, it doesn’t seem to work.

Please help.

Here is my code:

var array = [];
function rangeOfNumbers(startNum, endNum) {
if (startNum == endNum) {
   return array;
else {
   rangeOfNumbers(startNum + 1, endNum)
   return array;
console.log(rangeOfNumbers(1, 6))

Welcome, gbucar.

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

For future posts, if you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Sorry, I tried it and can’t figure it out. The output looks correct to me.

Maybe the tester doesn’t like that you used a global variable?

Hello and welcome to the fCC community~!

Yes, @CactusWren2020 is correct - the issue is your use of a global variable. More specifically, the issue is you have a console.log function call at the bottom, which fills your array with the range from 1 to 6. The issue is that value isn’t cleared when the tests are run, so the tests are seeing the wrong values.

The test suite is set up to “clean up” the results of each test, so the next test starts with a blank slate, but they won’t clean up the results from your function call. Try removing that console.log line and see what happens. :slight_smile:

1 Like

recursive functions must do atleast these 2 things:

  • return call itself
  • have an end condition
// example
function recurse(arg){
    if (condition){
    return recurse(arg + 1)


A recursive function doesn’t necessarily have to return itself, just call itself. :slight_smile:

Now that I removed console.log , everything works.

Thank you for your answer and explanation

1 Like

Hey, I don’t know if I should start a new thread for this. It’s the same exercise. Some of the solutions on the ‘Get a Hint’ page use syntax that we haven’t learned yet on fCC so I did it step by step with what I know. The tester accepted my solution but I feel it’s too bulky?

function rangeOfNumbers(startNum, endNum) {
  let n = endNum;
  if (n < startNum) {
    return [];
  } else {
    const countArray = rangeOfNumbers(startNum, n - 1);
    return countArray;
console.log(rangeOfNumbers(3, 9));

I pass my test but it feel not right (or complete) because I think it should take into account when the first number is bigger. Here is my solution:

function rangeOfNumbers(startNum, endNum) {
  if (endNum - startNum === 0) {
    return [startNum];
  } else if (startNum > endNum){
    var nums = rangeOfNumbers(endNum, startNum - 1);
    return nums;
  } else {
    var numbers = rangeOfNumbers(startNum, endNum - 1);
    return numbers;
console.log(rangeOfNumbers(14, 8));

the challenge does not ask to account for when the first number is bigger, but good job in covering edge cases! In this case it is not necessary tho

I’m closing this threas as it has become an invite to share solutions.

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.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.