# Why is count increasing

**URL:** <https://forum.freecodecamp.org/t/why-is-count-increasing/443488>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 30, 2021, 5:17pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488 "2021-01-30T17:17:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 30, 2021, 5:17pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/1 "2021-01-30T17:17:36Z")

</div>

**Tell us what’s happening:**  
i understand the basic principle here but i don’t quite get why it’s [1,2,3,4,5] (with n=5 and not [4,3,2,1,5]. seems 5-1=4 so that gets sent first, 4-1=3, 3-1=2, 2-1=1, 1-1=0 so push 5. what am i not getting?

**Your code so far**

```js

// Only change code below this line
//function countdown(n){
//return;
//}
// Only change code above this line

function countup(n) {
if (n < 1) {
  return [];
} else {
  const countArray = countup(n - 1);
  countArray.push(n);
  return countArray;
}
}
console.log(countup(5))

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (X11; CrOS x86_64 13421.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.199 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

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 30, 2021, 5:52pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/2 "2021-01-30T17:52:51Z")

</div>

Notice that `const countArray = countup(n - 1);` is before the push. At the time when push is executed `countArray` already contain all lower numbers, so `n` is added at the end. This applies also in the `countup(n - 1);` calls making numbers ordered appropriately.

---

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 30, 2021, 6:53pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/3 "2021-01-30T18:53:02Z")

</div>

yes i understand n being the last thing pushed into the array. but why are the first four digits in that order? why if n=5 is it 12345 not 43215?

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 30, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/4 "2021-01-30T18:58:29Z")

</div>

Base case of this recursion is `n < 1`, function keeps calling itself until that base case is reached. Only then it’s possible to start unraveling recursive calls.

So when `n < 1`, empty array is returned.  
Then call having `n = 1` can push to array and return `[1]`.  
Then call having `n = 2` can push to array and return `[1, 2]`.  
And so on, until all recursive calls finish, in this case ending up with `[1, 2, 3, 4, 5]`

---

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 31, 2021, 1:01pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/5 "2021-01-31T13:01:48Z")

</div>

I’m sure you’re right but I’m not getting it.  
Why is n = 1 performed before n = 2? Seems it should be n-1(4), then n-1-1(3), and so on until the base case is reached (n-1-1-1-1-1=0)  
which would push 4 into the empty array, then 3, 2, 1 , then n=5. so the array would look like [4,3,2,1,5]? I’m missing something.

---

<div class="post-metadata">

**Author:** ![amaniratna123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amaniratna123/32/145184_2.png) [@amaniratna123](https://forum.freecodecamp.org/u/amaniratna123)\
**Post date:** [January 31, 2021, 1:15pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/6 "2021-01-31T13:15:55Z")

</div>

Because you are using push instead of append  
so your first return is [5] , second is [4,5] and so on

---

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 31, 2021, 1:24pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/7 "2021-01-31T13:24:17Z")

</div>

Exactly…using push seems like it would push 4 into the empty array first, then 3, 2, 1 to get [4,3,2,1] and finally 5? I’m confused be the language of the explanation I guess.  
At first, this seems counterintuitive since the value of `n` _decreases_ , but the values in the final array are _increasing_ . This happens because the push happens last, after the recursive call has returned. At the point where `n` is pushed into the array, `countup(n - 1)` has already been evaluated and returned `[1, 2, ..., n - 1]`

---

<div class="post-metadata">

**Author:** ![amaniratna123](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amaniratna123/32/145184_2.png) [@amaniratna123](https://forum.freecodecamp.org/u/amaniratna123)\
**Post date:** [January 31, 2021, 1:32pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/8 "2021-01-31T13:32:19Z")

</div>

Sorry you are wrong push is happening before recursive call so your first pushed value is 5 in array. Push actually insert new value in front of array and append insert it in last

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 31, 2021, 1:34pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/9 "2021-01-31T13:34:29Z")

</div>

`n = 2` is called first, but it depends on the result it gets from `n = 1`, so without it it can’t finish and push `2` to the array. Until the base case is reached other calls can’t continue, because they depend on the result calculated by the recursive call.

```auto
// for n = 5
countArray = countup(5 - 1)
// for n = 4 
countArray = countup(4 - 1)
// for n = 3
countArray = countup(3 - 1)
// for n = 2
countArray = countup(2 - 1)
// for n = 1
countArray = countup(0)

// countup(0) is the first that can return result directly - empty array, so now it all can go in the reverse order:

// for n = 1
countArray = []
countArray.push(1)
// for n = 2
countArray = [1]
countArray.push(2)
// for n = 3
countArray = [1, 2]
countArray.push(3)
// for n = 4
countArray = [1, 2, 3]
countArray.push(4)
// for n = 5
countArray = [1, 2, 3, 4]
countArray.push(5)

```

---

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 31, 2021, 1:36pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/10 "2021-01-31T13:36:29Z")

</div>

I guess I’m wondering why, once the empty array is reached, they all go in reverse order.

---

<div class="post-metadata">

**Author:** ![sanity](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sanity/32/378897_2.png) [@sanity](https://forum.freecodecamp.org/u/sanity)\
**Post date:** [January 31, 2021, 1:44pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/11 "2021-01-31T13:44:23Z")

</div>

That’s simply following the order they were calling one another. `countup(0)` was called by `countup(1)`, `countup(1)` was called by `countup(2)`, etc.

Here’s nice tool that can somewhat help with visualization how this happens internally:  
[http://pythontutor.com/javascript.html#mode=edit](http://pythontutor.com/javascript.html#mode=edit)

---

<div class="post-metadata">

**Author:** ![croy4744](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/croy4744/32/192147_2.png) [@croy4744](https://forum.freecodecamp.org/u/croy4744)\
**Post date:** [January 31, 2021, 1:47pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/12 "2021-01-31T13:47:20Z")

</div>

I know sometimes it’s very difficult to understand program’s execution in the beginning as I am also a beginner and face this same issue quite frequently. I have broken down the given block of code for you to understand. Try to see how it works at each step by commenting parts below that point. Go through this and let me know if it helped. Also ignore the “undefined” as it’s because the function isn’t returning anything.

```auto

function countup(n) {
if (n < 1) {
  return [];
} else {
  //const countArray = countup(n - 1);
/*okay so, I will break each step for you 
to understand. Let's see how this program 
executes itself at each step. Program keeps
calling itself till the base case is reached 
where n = 1. At that point we have n = 1, 
n = 2, n = 3, n = 4 and n =5 in queue in that order.*/ 

 //Now first of all n = 1 
 let countArray=[];
  countArray.push(1);
  console.log(countArray);
  
  
  //Then n = 2 gets pushed
  countArray.push(2);
  console.log(countArray);
  
  
  //Then n = 3 gets pushed
  countArray.push(3);
  console.log(countArray);
  
  
  //Then n = 4 gets pushed;
  countArray.push(4);
  console.log(countArray);

  //Final element in queue i.e. n = 5 gets pushed at this moment after waiting so long. 

  countArray.push(5);
  console.log(countArray);

}
}
console.log(countup(5))

```

---

<div class="post-metadata">

**Author:** ![jkanavos](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jkanavos/32/155215_2.png) [@jkanavos](https://forum.freecodecamp.org/u/jkanavos)\
**Post date:** [January 31, 2021, 2:26pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/13 "2021-01-31T14:26:17Z")

</div>

Why is it in that order to begin with? Seems it should be in the opposite order since n=5 is the first one.

---

<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:** [January 31, 2021, 4:46pm UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/14 "2021-01-31T16:46:10Z")

</div>

We call `countup(5)`  
`countup(5)` pauses and calls `countup(4)`  
`countup(4)` pauses and calls `countup(3)`  
`countup(3)` pauses and calls `countup(2)`  
`countup(2)` pauses and calls `countup(1)`  
`countup(1)` pauses and calls `countup(0)`  
`countup(0)` returns `[]`  
`countup(1)` resumes, pushes 1, and returns `[1]`  
`countup(2)` resumes, pushes 2, and returns `[1, 2]`  
`countup(3)` resumes, pushes 3, and returns `[1, 2, 3]`  
`countup(4)` resumes, pushes 4, and returns `[1, 2, 3, 4]`  
`countup(5)` resumes, pushes 5, and returns `[1, 2, 3, 4, 5]`  
We receive `[1, 2, 3, 4, 5]`

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 2, 2021, 4:46am UTC](https://forum.freecodecamp.org/t/why-is-count-increasing/443488/15 "2021-08-02T04:46:22Z")

</div>

Closed on August 2, 2021, 4:46am UTC

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