# Recursion, why isn't this an acceptable answer?

**URL:** <https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 12, 2022, 8:09pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355 "2022-05-12T20:09:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mutiplify](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mutiplify/32/260024_2.png) [@Mutiplify](https://forum.freecodecamp.org/u/Mutiplify)\
**Post date:** [May 12, 2022, 8:09pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/1 "2022-05-12T20:09:58Z")

</div>

**Tell us what’s happening:**  
Am I missing something? Why is this not an acceptable recursive function?

```
  **Your code so far**

```

```js

// Only change code below this line
let arr = [];

function countdown(n){
if (n < 1){
  return arr;
} else {
	arr.push(n);
  countdown(n - 1);
  return arr.push(n);;
}
}
// 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/101.0.4951.64 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:** ![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 12, 2022, 8:10pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/2 "2022-05-12T20:10:13Z")

</div>

What does `return console.log(arr)` do?

---

<div class="post-metadata">

**Author:** ![Mutiplify](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mutiplify/32/260024_2.png) [@Mutiplify](https://forum.freecodecamp.org/u/Mutiplify)\
**Post date:** [May 12, 2022, 8:10pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/3 "2022-05-12T20:10:46Z")

</div>

Oops, that could be my issue. I was testing the function in Jsfiddle

---

<div class="post-metadata">

**Author:** ![Mutiplify](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mutiplify/32/260024_2.png) [@Mutiplify](https://forum.freecodecamp.org/u/Mutiplify)\
**Post date:** [May 12, 2022, 8:11pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/4 "2022-05-12T20:11:15Z")

</div>

Nope, even returning arr is still not passing

---

<div class="post-metadata">

**Author:** ![Mutiplify](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mutiplify/32/260024_2.png) [@Mutiplify](https://forum.freecodecamp.org/u/Mutiplify)\
**Post date:** [May 12, 2022, 8:13pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/5 "2022-05-12T20:13:45Z")

</div>

I found my error! Nevermind, sorry to bother everyone.

---

<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 12, 2022, 8:14pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/6 "2022-05-12T20:14:02Z")

</div>

Also, you are jamming everything into a global variable instead of using the return value of the recursive function call. This means you can only run your function once, since the global variable fills up with past data.

---

<div class="post-metadata">

**Author:** ![Mutiplify](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mutiplify/32/260024_2.png) [@Mutiplify](https://forum.freecodecamp.org/u/Mutiplify)\
**Post date:** [May 12, 2022, 8:15pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/7 "2022-05-12T20:15:12Z")

</div>

Oh you are right, I just tested that. So I need to make a local 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 12, 2022, 8:16pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/8 "2022-05-12T20:16:00Z")

</div>

Yup. Keep everything scoped to the function call.

---

<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:** [May 12, 2022, 9:12pm UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/9 "2022-05-12T21:12:46Z")

</div>

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

```javascript
var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

```

---

<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:** [November 11, 2022, 9:13am UTC](https://forum.freecodecamp.org/t/recursion-why-isnt-this-an-acceptable-answer/508355/10 "2022-11-11T09:13:34Z")

</div>

Closed on November 11, 2022, 9:13am UTC

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