Basic JavaScript: Use Recursion to Create a Countdown. Tests 2 and 3 are failing

This code seems to have the correct output in Repl.it and in the freeCodeCamp console, but it’s unable to pass tests 2 and 3. I am using Chrome. Thanks for any help.

//Only change code below this line
function countdown(myArray, n){  
  var num = n;
  
  if (n <= 0) {
    if (n === -1) {
      return [];
    }
    else {
      return myArray.concat([]);
    }
  }
  else {
    var newArr = countdown(myArray, n - 1);
    
    newArr.splice(myArray.length, 0, num);

    return newArr;
  }
}

console.log(countdown([10, 12], 5));

Link to the lesson?
also place your code between the “” :3

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

this is a recursion lesson
what bassicly is recursion is the next
1
1 1+
1 1+ 1++
our negative
10
10 10-
10 10- 10–
they also asking u to make a countdown
using the number that must include 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
but without using loops.
does your code equals that?

1 Like

I believe my code does that. It creates a list of descending values and adds it to the array. It creates a new array with all of the added values.

the lesson wants you to append the countdown to the input array, as in, change the input array

1 Like

Thanks a lot! I got it. I was creating a new array, but now I just modified the original.

My new solution:

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

    return myArray;
  }
}

console.log(countdown([10, 12], 10));

FYI - You don’t need to create another variable num. Just use the n variable. Also, see if you can think of a shorter way of writing (n === -1) || (n === 0).

1 Like

Thanks a lot! I revised my solution.