Answers for "how i calculate fibonacci number javascript"

5

how to generate a fibonacci sequence in javascript

// declare the array starting with the first 2 values of the fibonacci sequence
    let fibonacci = [0,1];
    
    function listFibonacci(num) {
    // starting at array index 1, and push current index + previous index to the array
        for (let i = 1; i < num; i++) {
            fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
        }
        console.log(fibonacci);
    }
    
    listFibonacci(10);
Posted by: Guest on July-13-2020
2

Fibonacci Sequence in javascript

let fibo = [0, 1]
for (var i = 2; i <= 10; i++) {
    fibo[i] = fibo[i - 1] + fibo[i - 2];
}
console.log(fibo)
//Output: [ 0, 1,  1,  2,  3,5, 8, 13, 21, 34, 55]
Posted by: Guest on December-16-2021

Code answers related to "how i calculate fibonacci number javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language