Problem challenge JS Recursion Countdown

Hello everybody !

I have a small problem on the “Basic JavaScript: Use Recursion to Create a Countdown” challenge :

When I run the Test, it returns:
countdown (10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown (5) should return [5, 4, 3, 2, 1]
However, my console.log shows me the right result.

My code :

var countArray = []
function countdown(n){
  if (n <= 0) {
    return [];
  } else {
    countArray.push(n)
    countdown(n-1);
    return countArray;
  }
}

I respect all the conditions:

  • countdown (-1) should return an empty array.
  • countdown (10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  • countdown (5) should return [5, 4, 3, 2, 1]
  • Your code should not rely on any kind of loops (for, while or higher order functions such as forEach, map, filter, and reduce).
  • You should use recursion to solve this problem.

Can you explain to me why I get an error? If I’m wrong, don’t hesitate to tell me :slight_smile:

Sorry in advance if the topic is in the wrong subforum.

Thanks,

Well, On the challenge at the bottom of the code there is

console.log(countup(5));

Which is not declared as a variable
what you should do is not use

but instead, put it at the end of the statement like this

function countdown(n){
  if (n >= 1) {
    countArray.push(n)
    countdown(n-1);
    return countArray;
  }
  return [];
}

Thank you for your answer Catalactics.
However, I do not understand why it is incorrect.
What do you mean by “Which is not declared as a variable”?

Well when I opened the challenge

console.log(countup(5));

was there.
But maybe I misunderstood, what did the error said?

The error said:

// running tests
countdown(10) should return [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
countdown(5) should return [5, 4, 3, 2, 1]
// tests completed

But when I remove console.log (countdown (10)) or countdown (10), I pass the challenge.
I do not understand why

owh, thats not an error, it just means that your code is wrong and did not pass the test. I don’t really see anything wrong with your code. I tried it and it worked well.
But here is the solution for that challenge if it still doesn’t works
Since it is a countdown instead of countup you dont use .push. instead use
.unshift so it adds the element to the start of the array instead of the end.
your code is right, and it actually works if you tried it. So countArray.push(n) to countArray.unshift(n).

When you call recursion, you need to do it like this: countArray = countdown(n-1); and right below that line put countArray.unshift(n)

You are using a global variable, which means your code isn’t really recursion. In general, you should not use global variables like this.

To see the problem, call countdown(10) and then countdown(5). You’ll see the results of the two melded together.

You need to modify your code to return an array and modify that array.

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