Last active
October 4, 2016 12:51
-
-
Save Shemeikka/af1a60b508a72436e315c4d73c8a4b09 to your computer and use it in GitHub Desktop.
factorial and sum of fibonacci(n) using recursion in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const factorial = (n, acc=1) => { | |
if (n < 1) { return undefined; } | |
if (n === 1) { return acc; } | |
return factorial(n-1, n*acc); | |
}; | |
const sumFibonacci = (n, prev=0, next=1, acc=0) => { | |
if (n === 0) { return acc; } | |
return sumFibonacci(n-1, next, next+prev, acc+prev+next); | |
}; | |
factorial(5); // 120 | |
sumFibonacci(6); // 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment