# Need help with Basic JavaScript: Replace Loops using Recursion

**URL:** <https://forum.freecodecamp.org/t/need-help-with-basic-javascript-replace-loops-using-recursion/407478>\
**Category:** General\
**Created:** [July 1, 2020, 9:40pm UTC](https://forum.freecodecamp.org/t/need-help-with-basic-javascript-replace-loops-using-recursion/407478 "2020-07-01T21:40:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dustinmcleod98](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dustinmcleod98/32/166646_2.png) [@dustinmcleod98](https://forum.freecodecamp.org/u/dustinmcleod98)\
**Post date:** [July 1, 2020, 9:40pm UTC](https://forum.freecodecamp.org/t/need-help-with-basic-javascript-replace-loops-using-recursion/407478/1 "2020-07-01T21:40:22Z")

</div>

I understand the initial if statement. I don’t understand the else statement. What exactly is it doing? I need an in depth explanation. I know it’s a lot to ask but sometimes i come across a lesson and the best way for me to understand is explanations from other people. Thank you!!

```auto
function sum(arr, n) {
  // Only change code below this line
 if(n <= 0) {
    return 0;
  } else {
    return sum(arr, n - 1) + arr[n - 1];
  }
  // Only change code above this line
}

```

---

<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:** [July 1, 2020, 10:04pm UTC](https://forum.freecodecamp.org/t/need-help-with-basic-javascript-replace-loops-using-recursion/407478/2 "2020-07-01T22:04:10Z")

</div>

There have been a few really good and in-depth discussions of how recursion works here on the forum, where people have drawn diagrams, shared their favorite articles and videos, and stepped through the process of some of the freeCodeCamp recursive functions. I’m not trying to discourage you from asking questions, but since you are generally asking for a broad explanation, I suggest reading through past explanations (the forum search feature is very good) so that you can ask more specific questions about the parts that don’t make sense to you.

Here are a couple:

> [@Recursion: What is actually happening here?](http://forum.freecodecamp.org/t/recursion-what-is-actually-happening-here/343528):
>
> Ok, my problem isn’t passing the challenge. My problem is understanding why the code in the challenge example actually does what it does. I don’t want to write code I don’t understand just to pass the challenge and move forward. I want to actually understand what is going on behind the scenes so to speak. I left a few comments on the code below to help outline my confusion. I tried writing things out on paper but that got confusing. It seems to me that countArray doesn’t actually become an arra…

> [@Cannot understand how Function Recursion Works on this Problem (even if I figured out the solution)](http://forum.freecodecamp.org/t/cannot-understand-how-function-recursion-works-on-this-problem-even-if-i-figured-out-the-solution/344143):
>
> Tell us what’s happening: Code works fine. There are no problems with execution or whatsoever. I just cannot understand how this recursion works, on this block: else { const countArray = countdown(n - 1); console.log(`n: ${n}, countArray ${countArray}`); countArray.unshift(n); return countArray; The first line that is getting executed is: const countArray = countdown (n - 1); which as far as I can tell this the point that the variable recalls the function and as I far as I know, w…

---

<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, 6:49pm UTC](https://forum.freecodecamp.org/t/need-help-with-basic-javascript-replace-loops-using-recursion/407478/3 "2021-06-01T18:49:23Z")

</div>

Closed on June 1, 2021, 6:49pm UTC


