fibonacci javascript easy code example
Example 1: javascript fibonacci example
function fibonacci(nums) {
let fib = [0, 1];
let data = [];
for(let i = 2; i <= nums; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
data.push(fib[i]);
}
return data;
}
Example 2: fibonacci best performance javascript
function fibonacci(n) {
const list = [0, 1];
for (let x = 2; x < n + 1; x += 1) {
list.push(list[x - 2] + list[x - 1]);
}
return list[n];
}
console.log(fibonacci(4));
Example 3: 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]);
}