# Use Recursion to Create a Countdown, Solution 4

**URL:** <https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [April 21, 2020, 3:44pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789 "2020-04-21T15:44:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathias](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mathias/32/134249_2.png) [@mathias](https://forum.freecodecamp.org/u/mathias)\
**Post date:** [April 21, 2020, 3:44pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/1 "2020-04-21T15:44:19Z")

</div>

Hi everyone, this is my first post, happy to be here at freecodecamp, awesome site!  
After doing some forum searching, I am slowing wrapping my head around recursion, but solution 4 is the closest thing to coding black magic since 0x5F3759DF.  
To recap, the solution is:

```auto
function countdown(n){
   return n < 1 ? [] : [n, ...countdown(n - 1)];
}

```

Could someone please explain this, especially the effect of the ellipses?  
Thank you!

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 21, 2020, 3:49pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/2 "2020-04-21T15:49:32Z")

</div>

Welcome, mathias.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

**Note:** Backticks are not single quotes.

![markdown_Forums](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [April 21, 2020, 3:50pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/3 "2020-04-21T15:50:49Z")

</div>

Are you familiar with ternaries?

```auto
n < 1 ? [] : [n, ... countdown(n - 1)]

```

is a ternary expression that will evaluate to `[]` when `n < 1` or `[n, ...countdown(n - 1)]` otherwise.

The syntax

```auto
[n, ...countdown(n - 1)]

```

builds an array that starts with `n` and contains the `spread` contents of `countdown(n - 1)`.

The rest of the logic is the same as the examples with the `if` statement and multiple `return` statements.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 21, 2020, 3:53pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/4 "2020-04-21T15:53:16Z")

</div>

In terms of wrapping your head around recurrsion, the topic was discussed at length here: [Replace Loops using Recursion -1 explanation](http://forum.freecodecamp.org/t/replace-loops-using-recursion-1-explanation/368021)

> [@mathias](#):
>
> Could someone please explain this, especially the effect of the ellipses?

The use of the ellipses is found in this lesson: [https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place)

I hope these links help somewhat. If not, feel free to ask for clarification on anything.

---

<div class="post-metadata">

**Author:** ![mathias](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mathias/32/134249_2.png) [@mathias](https://forum.freecodecamp.org/u/mathias)\
**Post date:** [April 21, 2020, 3:58pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/5 "2020-04-21T15:58:15Z")

</div>

Hi Jeremy,

I am as yet unfamiliar with ternaries, but will investigate further, along with spread contents (I hope that is what I google).  
EDIT: Actually the ternary was fine, I just didn’t recall ‘?’ being called that 🙂

Thanks for the speedy reply.

---

<div class="post-metadata">

**Author:** ![mathias](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mathias/32/134249_2.png) [@mathias](https://forum.freecodecamp.org/u/mathias)\
**Post date:** [April 21, 2020, 4:01pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/6 "2020-04-21T16:01:31Z")

</div>

Hi Sky,

Thanks for the syntax correction for code.

The linked articles explained each issue perfectly.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 12:57pm UTC](https://forum.freecodecamp.org/t/use-recursion-to-create-a-countdown-solution-4/376789/7 "2021-06-01T12:57:01Z")

</div>

Closed on June 1, 2021, 12:57pm UTC


