DeveloperBreeze

Fibonacci Sequence in JavaScript

javascript
// Function to calculate the nth Fibonacci number iteratively
function fibonacci(n) {
    if (n <= 1) return n;

    let a = 0, b = 1;
    for (let i = 2; i <= n; i++) {
        let temp = a + b;
        a = b;
        b = temp;
    }

    return b;
}

// Calculate the 10th Fibonacci number
let nthFibonacci = fibonacci(10);

Continue Reading

Discover more amazing content handpicked just for you

Code
php

Recursive Factorial Calculation in PHP

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate Fibonacci Sequence

No preview available for this content.

Jan 26, 2024
Read More
Code
java

Calculate Factorial

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!