# I do not understand what's goin on here

**URL:** <https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 6, 2020, 5:43am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462 "2020-05-06T05:43:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 6, 2020, 5:43am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/1 "2020-05-06T05:43:38Z")

</div>

Right now, I’m on another exercise of Javascript. And I took the liberty of clicking on I need a hint. As of now here is my code.

```js

// Only change code below this line
function countdown(n){
if (n < 1){
  return [];
}
  else{
      const countArray = countdown(n-1);
      countArray.unshift(n);
      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/83.0.4103.34 Safari/537.36`.

**Challenge:** Use Recursion to Create a Countdown

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-recursion-to-create-a-countdown)**
>
> Learn to Code — For Free

I do not understand why the value of ‘n’, being placed **after** the previous value of ‘n’ via ‘unshift’, isn’t unshift supposed to place a new value ‘infront’ of the array? If so, why are they being placed after the previous value of ‘n’ in the array? @\_@

---

<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:** [May 6, 2020, 5:45am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/2 "2020-05-06T05:45:55Z")

</div>

Unshift adds elements to the beginning of the array.

---

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 6, 2020, 6:27am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/3 "2020-05-06T06:27:33Z")

</div>

Yeah, It’s supposed to behave that way. But I still don’t understand why the result of the recursive function is being placed after the array…

---

<div class="post-metadata">

**Author:** ![Naomi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/naomi/32/422181_2.png) [@Naomi](https://forum.freecodecamp.org/u/Naomi)\
**Post date:** [May 6, 2020, 6:35am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/4 "2020-05-06T06:35:05Z")

</div>

If I’m understanding your question correctly… What you’re seeing is the “stack”.

```auto
countdown(5) --> shift(5), countdown (4)
>>countdown(4) --> shift(4), countdown(3)
>>>>countdown(3) --> shift(3), countdown(2)
>>>>>>countdown(2) --> shift(2), countdown(1)
>>>>>>>>countdown(1) --> shift(1), countdown(0)
>>>>>>>>>>countdown(0) --> []

```

Once the stack is built, it begins resolving with the LAST item added to the stack.

```auto
>>>>>>>>>>[]
>>>>>>>>shift(1) -> [1]
>>>>>>shift(2) -> [2, 1]
>>>>shift(3) -> [3, 2, 1]
>>shift(4) -> [4, 3, 2, 1]
shift(5) -> [5, 4, 3, 2, 1]

```

Does that make sense? And answer your question?

---

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 6, 2020, 8:03am UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/5 "2020-05-06T08:03:55Z")

</div>

Ah, I understand, that’s why it’s happening like that. It seems that I’ll have to repeat the lessons again, since I’m still finding it hard to understand the concepts,

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 6, 2020, 2:45pm UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/6 "2020-05-06T14:45:07Z")

</div>

The thing that takes a while to switch your head over to is that _recursion is not a loop_.

You’re used to solving numbers with loops, where you repeat a sequence of commands within a set context. Recursion is not a looping function. Every time a function gets called, it is called in isolation. It does not carry context with it. It doesn’t “know” whether it was called recursively or not. Multiple instances of the function are running separately.

---

<div class="post-metadata">

**Author:** ![olibadua](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/olibadua/32/146438_2.png) [@olibadua](https://forum.freecodecamp.org/u/olibadua)\
**Post date:** [May 8, 2020, 2:19pm UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/7 "2020-05-08T14:19:19Z")

</div>

I see, it’s like an endless loop of Deja Vu.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [May 8, 2020, 4:18pm UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/8 "2020-05-08T16:18:33Z")

</div>

Recursion:

 ![Groundhog Day movie poster](https://global.discourse-cdn.com/freecodecamp/original/3X/5/7/5766c1374d0e1288f0f1fcdebb43b6c659ffce0b.jpeg)

---

<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:55pm UTC](https://forum.freecodecamp.org/t/i-do-not-understand-whats-goin-on-here/384462/9 "2021-06-01T12:55:48Z")

</div>


