Use Recursion to Create a Countdown_6.21.21

Hello, In this exercise I have to write a function called countDown where it takes in an argument of type integer (n) and will print out n - 1 in descending order, so if 4 was passed in as (n) when I run code I should get 4, 3, 2, 1. So far in my code I have been able to simply print out these numbers however Im having an issue understanding how to push these numbers into a new array which is apart of what the original question wants me to do.

function countDown(n) {
  if (n < 1) {
    return [];
  } else {
    const countDecrease = countDown(n - 1);
    return countDecrease;

   **Your browser information:**

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

Challenge: Use Recursion to Create a Countdown

Link to the challenge:

if you use console.log(countDown(4)) you will notice your function returns [1,2,3,4], so you are pretty close. You must find a way to push the numbers in reverse, or maybe push the array which is retarned from calling your function to the previous n? if that makes sense :wink:

Lets say for example countDown(4) should return [4,3,2,1]. What countDown(5) should return? [5,4,3,2,1] So countDown(5) would be returning [5, ...countDown(4)] right? Just need to find a way how to write that in the function logic.


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