# Recursion function

**URL:** <https://forum.freecodecamp.org/t/recursion-function/79045>\
**Category:** General\
**Created:** [January 21, 2017, 3:56pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045 "2017-01-21T15:56:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 21, 2017, 3:56pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/1 "2017-01-21T15:56:33Z")

</div>

Please. Explain to me how this function works. Thank you.

 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/3/8/38afdec0f8f6a1eb6a90ab274612f91c91677d23.png)

---

<div class="post-metadata">

**Author:** ![joesmith100](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joesmith100/32/34933_2.png) [@joesmith100](https://forum.freecodecamp.org/u/joesmith100)\
**Post date:** [January 21, 2017, 4:16pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/2 "2017-01-21T16:16:07Z")

</div>

Hi @SerafimPoch

It’s essentially whittling down the number until it reaches either 0 or 1, 0 meaning its an even number, 1 being odd.

A simpler example might be to use `isEven(4)`.

1st call, 4 meets none of the if statements so it returns `isEven(2)`, 4 - 2 being the 2 in this case.  
2nd call, 2 is the same as 4 (no conditions met) so it returns `isEven(0)`  
3rd call, first if condition is true so it returns true, this bubbles up to the 1st call and the function ultimately returns true, 0 is an even number.

If we used `isEven(5)`, it would eventually meet the `n === 1` condition and return false, 5 is an odd number.

The `else if (n < 0)` is there to catch any negative numbers, it will turn it into a positive number: `-(-6) === 6`.

As a side note, you can calculate an even number by using modulo: `n % 2 === 0`. Much easier and less verbose.

---

<div class="post-metadata">

**Author:** ![bsroka](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bsroka](https://forum.freecodecamp.org/u/bsroka)\
**Post date:** [January 21, 2017, 4:19pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/3 "2017-01-21T16:19:11Z")

</div>

Essentially it will keep subtracting 2 from the number it is passed until the result is either 0, for even numbers, or 1 for odd. First, it’s checking if the number is already 0, then if it’s 1, then it makes sure the number is positive, and then it subtracts 2 and tries again. The last step of the function will keep calling itself until one of the first two statements is true, prompting it to return the boolean and exit the function.

---

<div class="post-metadata">

**Author:** ![bsroka](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@bsroka](https://forum.freecodecamp.org/u/bsroka)\
**Post date:** [January 21, 2017, 4:25pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/4 "2017-01-21T16:25:31Z")

</div>

I stepped away for coffee while writing my reply and you beat me to it, sorry for the repeat explanation.

---

<div class="post-metadata">

**Author:** ![singletary3](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@singletary3](https://forum.freecodecamp.org/u/singletary3)\
**Post date:** [January 21, 2017, 4:41pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/5 "2017-01-21T16:41:26Z")

</div>

Recursive functions break down larger problems into smaller ones.

This function takes a value n, checks for base cases which are:

**if (n == 0)** checks to see if n is equal to 0, if so the function returns true because it is even  
**else if (n == 1)** checks to see if n is equal to 1, if so the function returns false because it is odd.

The function will keep calling itself until **n** reaches 1 or 0.

For example: if the function receives the number **5** , it will not pass those two base cases because it is neither 1 or 0. The number **5** is also not less than 0, so it will not go to **return isEven(-n)** which would take a negative number and call the function again with that same number but turning it into a postive form. Now since it didn’t pass any of those, it would go to the **else** and you would get **return isEven(5 - 2);** which would call the function again with the number **3**.

Repeat the process and you should return back to that **else** and it would call the function once more with the value **1**. Then it would pass the base case of **else if (n == 1)** since it is equal to 1, returning the value **false** because the number 5 is not an even number. Hope that helps!

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 22, 2017, 2:57pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/6 "2017-01-22T14:57:04Z")

</div>

I understand. Thank you. But this part  
else if (n \< 0)  
return isEven(-n);  
I don’t. I guess I know what it does. But maybe be better if you explain in detail this part. Thanks

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [January 22, 2017, 3:02pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/7 "2017-01-22T15:02:25Z")

</div>

And way faster hahaha

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 22, 2017, 3:04pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/8 "2017-01-22T15:04:47Z")

</div>

I know how it do more easier) I’ve trying to understand how recursion works)

---

<div class="post-metadata">

**Author:** ![joesmith100](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joesmith100/32/34933_2.png) [@joesmith100](https://forum.freecodecamp.org/u/joesmith100)\
**Post date:** [January 22, 2017, 3:06pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/9 "2017-01-22T15:06:13Z")

</div>

If you excluded `else if (n < 0`) and passed a negative number in: `isEven(-4`), the first 2 conditions would never be met so the function would never return and be caught in an infinite loop.

```auto
-4 === 0 // false
-4 === 1 // false

return isEven(-4 - 2) // This would just continually takeaway 2 from n, eventually leading to a max stack exceeded error.

```

By having `else if (n < 0)`, it ensures that the above situation would never happen by turning the negative number into a positive: `isEven(-4)`, would then recursively call `isEven(4)` and continue till it equals 0 and returns true.

I hope that makes sense

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 22, 2017, 3:09pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/10 "2017-01-22T15:09:49Z")

</div>

How exactly it turns -50 into 50 ?

- 
  - = + ?

---

<div class="post-metadata">

**Author:** ![joesmith100](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joesmith100/32/34933_2.png) [@joesmith100](https://forum.freecodecamp.org/u/joesmith100)\
**Post date:** [January 22, 2017, 3:15pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/11 "2017-01-22T15:15:21Z")

</div>

Prefixing - to a number is essentially shorthand for [Math.abs(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs).

Note, directly prefixing a number with - won’t work, you’d have to wrap it in parens, or add a space:

```auto
--4 // Uncaught ReferenceError
-(-4) // 4
- -4 // 4
Math.abs(-4) // 4

```

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 22, 2017, 3:19pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/12 "2017-01-22T15:19:46Z")

</div>

Oyyy I understand)) Thank you for your time)

---

<div class="post-metadata">

**Author:** ![joesmith100](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joesmith100/32/34933_2.png) [@joesmith100](https://forum.freecodecamp.org/u/joesmith100)\
**Post date:** [January 22, 2017, 3:20pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/13 "2017-01-22T15:20:10Z")

</div>

No worries dude, happy to help

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [January 22, 2017, 3:28pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/14 "2017-01-22T15:28:20Z")

</div>

Maybe you recommend to me a better way to learn Js ? Because I’ve learn Js for two month but write functions it’s difficult to me. I undedrstand how functions works, but wrtite them to myself is kind of awkward 😓

---

<div class="post-metadata">

**Author:** ![joesmith100](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/joesmith100/32/34933_2.png) [@joesmith100](https://forum.freecodecamp.org/u/joesmith100)\
**Post date:** [January 22, 2017, 3:40pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/15 "2017-01-22T15:40:19Z")

</div>

I don’t really know of a better way to be honest, most of my learning came from doing with the guidance of various resources including freecodecamp.

These posts might be a good place to start:  
[https://forum.freecodecamp.com/t/computer-guide-computer-science-and-web-development-comprehensive-path/64470](https://forum.freecodecamp.com/t/computer-guide-computer-science-and-web-development-comprehensive-path/64470)  
[https://forum.freecodecamp.com/t/computer-guide-video-game-programming-comprehensive-foundations/69301](https://forum.freecodecamp.com/t/computer-guide-video-game-programming-comprehensive-foundations/69301)

---

<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:** [January 16, 2021, 5:10pm UTC](https://forum.freecodecamp.org/t/recursion-function/79045/16 "2021-01-16T17:10:51Z")

</div>

Closed on January 16, 2021, 5:10pm UTC


