Basic JavaScript: Use Recursion to Create a Countdown

Tell us what’s happening:
Hello there, this is my first post.
The test says I’m wrong, whereas I’m pretty sure that i’m being right, i already tested it in another editor and it work. So i don’t know where is the probleme, maybe it has something to do with the way it’s tested.

Your code so far


//Only change code below this line

function countdown(myArray, n){
if (n == 1) {
  return [1];
} else if (n > 1) {
  myArray = countdown(myArray, n-1);
  myArray.unshift(n);
 return myArray;
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown

You have made it too complicated:

function countdown(myArray, n){
  if (n >= 1) {
    myArray = countdown(myArray, n-1);
    myArray.unshift(n);
  }
  return myArray;
}

If you want to leave your code as is, then what is messing up the test is the fact that you are just returning [1] as apposed to shifting it into your myArray variable.

5 Likes

Oh i didn’t think about the fact that i return somthing, in the challenge they do not say somthing about returning any value. My bad and thank you

If you don’t mind I will use your post. Why my solution does not work but locally in VS Code everything is OK

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

why do I get this error when I run it in chrome console?

function countdown(myArray, n){

  if (n <= 0) {

    return [];
  } else {
    myArray.push(n);
    countdown(myArray, n - 1);
  }
};
countdown(myArray, 5);

VM707:9 Uncaught ReferenceError: myArray is not defined
at :9:11

you have myArray as function parameter, so a certain value is passed in as function argument, but then you make a new variable with the same name that is an empty array essentially overwriting previous variable

1 Like

you call the function with a myArray as argument, but there is not a global myArray defined anywhere

2 Likes

countdown(myArray, 5); is outside of the function definition and is actually calling the function. You should use:

function countdown(myArray, n){
...
};
const myArray = [];
countdown(myArray, 5);

which defines a myArray variable outside of the function in the global scope and is available to the rest of your program. Or, if you don’t plan to save the results, you can simply use:

function countdown(myArray, n){
...
};
countdown([], 5);
function countdown(myArray, n) {
  if (n <= 0) {
    return [];
  } else {
    const myArray = countdown(myArray, n - 1);
    myArray.unshift(n);
    return myArray;
  }
}

Why is this solution wrong?

I copy/pasted your code into the Chrome console and got the following error:

Uncaught ReferenceError: Cannot access 'myArray' before initialization
    at countdown (<anonymous>:5:31)
    at <anonymous>:1:1

I then changed the last three lines as follows:

    const numbers = countdown(myArray, n - 1);
    numbers.unshift(n);
    return numbers;

In your line, const myArray = countdown(myArray, n - 1); you are attempting to define a block-level variable named myArray, but you are also referencing myArray in the expression, countdown(myArray, n - 1). Now, you and I know you meant the parameter myArray that was passed into the function, but the interpreter saw it as referencing the new variable you were creating.

The best thing is to use a different name for the new array. I chose the name numbers, but any name but myArray (or n) would do.

2 Likes

Hey this was the solution and it did not pass tests 2 and 3:

function countdown(myArray, n) {
  if (n <= 0) {
    return [];
  } else {
    const numbers = countdown(myArray, n - 1);
    numbers.unshift(n);
    return numbers;
  }
}

This is the solution that FCC lists:

function countdown(myArray, n) {
  if (n <= 0) {
    return;
  } else {
    myArray.push(n);
    countdown(myArray, n - 1);
  }
}

when i run either of these in the VS Code/Chrome console with console.log(countdown(myArray, 10)); i get the error ReferenceError: myArray is not defined but if i call the function with console.log(countdown(Array, 10)); my original solution works as intended but FCC’s solution then gives me another error TypeError: myArray.push is not a function.

Why is my solution wrong on FCC and FCC’s solution is wrong on VS Code/Chrome console? and why cant I console log the test case FCC lists countdown(myArray, 10) for either of these solutions on VSCode/Chrome console without an error?

1 Like

Still curious about my last question…any help would be appreciated, thanks.

myArray is local to the countdown function, so if you try to call (from the global scope) the function with a variable named myArray as the first argument, you will get an error something like “myArray not defined”.

2 Likes

I think the confusion comes from the fact that the tests’s function calls pass in myArray as an argument, and if someone try to just copy and paste those to test, one get a Reference Error

2 Likes

What if the tests were written like the following? Would that help?

After calling countdown([], -1) , myArray should be empty.

After calling countdown([], 10) , myArray should contain [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

After calling countdown([], 5) , myArray should contain [5, 4, 3, 2, 1]

2 Likes

I would be really confused as to which myArray the test would be referencing… It can’t be the function parameter as that is local to the function and so the tests can’t check, right?
saying “the input array” would be less ambiguous, I think. Maybe not less confusing, tho
I say no less confusing because this challenge, after a series of challenges that have a function with a clear input and a clear output, present a situation in which you need to change the input, and there is no output. It sort of breaks the pattern, without stating it clearly, and makes hella confusing the test requirements.

Yeah, this particular challenge was not one that I thought should be included. I think we need to rethink this particular challenge or remove it completely as it does not flow like the other two recursion challenges.

1 Like

I think it should be included somewhere, to present a totally different situation, but it needs or a different position, or some more explanation

2 Likes

I have a couple of ideas. I will create a PR and you can review it when I make the changes.

1 Like

@ilenia @ArielLeslie I have created PR #37548 which attempts to rectify some of the the confusion in the Use Recursion to Create a Countdown. You can click on the Review in Gitpod and view the challenge rewrite as it would appear in production. I welcome any comments or suggestions for the challenge. I literally spent about 5 minutes on the rewrite.