fibonacci program in javascript 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: fibonacci javascript
function Fibonacci(valor){
var anterior = 0;
var atual = 1;
var proximo = 1;
for(let i = 0; i < valor; i++){
console.log(proximo)
anterior = atual + proximo;
atual = proximo
proximo = anterior
}
}
Fibonacci(100);
Example 5: 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]);
}
Example 6: recursive function for fibonacci series in java javascript
var fib = function(n) {
if (n === 1) {
return [0, 1];
} else {
var arr = fib(n - 1);
arr.push(arr[arr.length - 1] + arr[arr.length - 2]);
return arr;
}
};
console.log(fib(8));