Skip to content

Instantly share code, notes, and snippets.

@arifulhb
Created October 9, 2020 13:11
Show Gist options
  • Save arifulhb/959d2948031065ed0a2cad1e52775059 to your computer and use it in GitHub Desktop.
Save arifulhb/959d2948031065ed0a2cad1e52775059 to your computer and use it in GitHub Desktop.
Print Fibonacci series using recursive function
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