Recursion to Create a Countdown

Tell us what’s happening:
The code produces the required answer but for some reason it fails the test. Does anyone know what the problem is

Your code so far


//Only change code below this line
function countdown(myArray, n){
 } else if (n==1) {
   return [1] ;
 } else {
myArray = countdown(myArray,n-1);
 return myArray;
var myArray;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

Hello @Hlon18 :grinning:

  1. myArray is an array so instead of var myArray; write like - var myArray=[];
    or var myArray= new Array();

  2. After this your if condition is ok. else if when n===1 , try to use push in whole code and push 1 in myArray when n===1, like return myArray.push(n);

  3. Last else condition remove line - myArray = countdown(myArray,n-1);
    write return like -return countdown(myArray, n-1);
    and also use myArray.push(n)
    instead of myArray.unshift(n);

It worked! Thank you so much, I now understand my error.

1 Like

:grinning: welcome, Happy Coding.