recursive fibonaci js code example
Example 1: javascript fibonacci
function fibonacci(n) {
return n < 1 ? 0
: n <= 2 ? 1
: fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(4));
Example 2: fibonacci sums javascript
// Implement a method that finds the sum of the first n
// fibonacci numbers recursively. Assume n > 0
function fibsSum(n) {
if ( n === 1 ) {
return 1;
}
if (n === 2 ) {
return 2;
}
let sum = fibsSum(n-1) + n;
return sum;
}