Created
October 9, 2020 13:11
-
-
Save arifulhb/959d2948031065ed0a2cad1e52775059 to your computer and use it in GitHub Desktop.
Print Fibonacci series using recursive function
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
function fib(num) { | |
let sequence = [0, 1] | |
function helper(sequence, num) { | |
const length = sequence.length | |
// get sum of last two numbers | |
let sum = sequence[length - 1] + sequence[length - 2] | |
if (sequence.length > num) { | |
return sequence | |
} | |
sequence.push(sum) | |
helper(sequence, num) | |
return sequence | |
} | |
helper(sequence, num) | |
return sequence | |
} | |
fib(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment