Recursion Countdown Help!

That’s what I have for the solution to the problem, and it seems to output the correct result except for the fact that there’s an extra space in front of the first element and behind the last element (at least that’s how it appears in the the console). Not sure why my code isn’t correct. Help!

Your code so far

//Only change code below this line
var countArray = [];
function countdown(n){
if (n < 1) {
  return [];
else {
  countdown(n - 1);
  return countArray;
console.log(countdown(5)); // [5, 4, 3, 2, 1]

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.2 Safari/605.1.15.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

What’s gonna happen if test will run your function multiple times?

Could you clarify what you meant?
I’m confused as to what you mean by multiple times. The function runs and stop running once n is less than 1, returning countArray after that. Right?

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.


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

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

(@vivek-agrawal27 if you say there is nothing wrong, then the issue with global variables is a thing you also need to learn)


@taherfa Everything is fine with your code. It’s something in the fCC console.

Yes you are right. I totally agree with you. Thanks for explaining this concept :slight_smile:

@taherfa Your code is logically correct for a single input. But when we start giving a set of inputs they will give wrong results because they are altering a global variable. Try logging countdown(5) and countdown(10) to the console. You will get @ilenia’s point.

1 Like

@taherfa You need to refactor your code as we suggested above to pass all the tests. But the space thing you are talking about is an issue with the fCC console I believe.

@RandellDawson Should we open an issue on GitHub regarding this?

1 Like

@taherfa @vivek-agrawal27

if the extra space is like this:

then the issue is non existent, the spaces between elements in an array do not have a meaning, it is just a visual thing
so these are all the same:

[ 5, 4, 3, 2, 1 ]
[ 5,4,3,2,1 ]
// etc. etc. etc.
1 Like

@ilenia Indeed they all are same. fCC team might have done it for readability purposes. But it may confuse campers as it did in this case, that’s why I thought of discussing this on GitHub.

I see, thanks for the explanation!

No need to create an issue on GitHub.