fibonacci series js function code example
Example 1: how to generate a fibonacci sequence in javascript
let fibonacci = [0,1];
function listFibonacci(num) {
for (let i = 1; i < num; i++) {
fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
}
console.log(fibonacci);
}
listFibonacci(10);
Example 2: fibonacci javascript
function Fibonacci(num){
var before = 0;
var actual = 1;
var next = 1;
for(let i = 0; i < num; i++){
console.log(next)
before = actual + next;
actual = next
next = before
}
}
Fibonacci(100);
Example 3: javascript fibonacci
function fibonacci(n) {
return n < 1 ? 0
: n <= 2 ? 1
: fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(4));
Example 4: js fibonacci sequence
var i;
var fib = [];
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= 10; i++) {
fib[i] = fib[i - 2] + fib[i - 1];
console.log(fib[i]);
}