Basic JavaScript - Use Recursion to Create a Countdown

Tell us what’s happening:
Describe your issue in detail here.
I am trying to write a recursive function that has one argument and returns an array of n in descending order starting with n

Your code so far
function countdown(n){
const w = n;
if (w < 1){
return ;
}else{
const countArray = countdown(w - 1);
countArray.push(w);
return countArray;
}
}

// Only change code below this line
function countdown(n){
  const w = n;
  if (w < 1){
    return [];
  }else{
    const countArray = countdown(w - 1);
    countArray.push(w);
    return countArray;
  }
}
// Only change code above this line

Your browser information:

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

Challenge: Basic JavaScript - Use Recursion to Create a Countdown

Link to the challenge:

Hi there!

You want the numbers to count DOWN == add the results of (n - 1) at the start of the array.

Remember the basic lessons, what is the function that adds something like an integer at the start of an array?

Yeah I have used unshift and it has worked thanks alot

The ‘push’ method create ascending order of elements. Use the different method to create a descending order (a method that adds new elements to the beginning of an array):

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.