Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js how to get n fibonacci number

function getFibonacci(n) {
    let a = 1;
    let b = 1;

    for (let i = 3; i <= n; i++) {
      let c = a + b;
      a = b;
      b = c;
    }

    return b;
  }

  console.log(getFibonacci(3)); // => 2
  console.log(getFibonacci(7)); // => 13
  console.log(getFibonacci(20)); // => 6765
  console.log(getFibonacci(77)); // => 5527939700884757
 
PREVIOUS NEXT
Tagged: #js #fibonacci #number
ADD COMMENT
Topic
Name
8+3 =