Created
December 21, 2020 18:19
-
-
Save neuralline/87a017dd2416cdf191d1801cd5ab23ea to your computer and use it in GitHub Desktop.
Fibonacci recursive function using arrays and destructuring
This file contains 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
/** | |
* Javascript Algorithms. | |
* Fibonacci function using arrays and recursive methods. | |
* Licence: Don't use this code for anything ever! :) | |
* But if you do, give credit where credit is due. | |
* @author Darik. | |
* @GitHub @neuralline | |
* | |
* | |
* | |
* @param {number} n number of times | |
* @param {[]} remainder do not assign | |
* | |
*/ | |
const fibonacci = (n = 0, remainder = [1, 1]) => { | |
const l = remainder.length | |
return l >= n | |
? remainder | |
: fibonacci(n, [...remainder, remainder[l - 2] + remainder[l - 1]]) | |
} | |
module.exports = fibonacci |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment