Skip to content

Instantly share code, notes, and snippets.

@neuralline
Created December 21, 2020 18:19
Show Gist options
  • Save neuralline/87a017dd2416cdf191d1801cd5ab23ea to your computer and use it in GitHub Desktop.
Save neuralline/87a017dd2416cdf191d1801cd5ab23ea to your computer and use it in GitHub Desktop.
Fibonacci recursive function using arrays and destructuring
/**
* 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