JavaScript program that returns the nth entry in the Fibonacci sequence
Posted by SceDev
Last Updated: February 27, 2024

Write a JavaScript program that returns the nth entry in the fibonacci sequence.

Code:

function getNthFibonacci(n) {
    const fib = [0, 1];
    for (let i = 2; i <= n; i++) {
      fib[i] = fib[i - 1] + fib[i - 2];
    }
    return fib[n];
  }

var result = getNthFibonacci(8)

console.log(result);