Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js get fibonacci number

/**
 * Method that calculates the Fibonacci Sequence and returns the n'th value of the sequence
 * @param {*} pos 
 */
const getFibPosition = (pos) => {
    if (pos === 0) return null;
    if (pos === 1) return 0;
    if (pos === 2) return 1;

    let fibArray = [0, 1];
    let aux1, aux2;

    for (let i = 2; i < pos; i++) {
        aux1 = fibArray[i - 1];
        aux2 = fibArray[i - 2];

        fibArray.push(aux1 + aux2);
    }

    return fibArray[pos - 1];
}
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: check for null 
Javascript :: react native android build location 
Javascript :: EACCES: permission denied /delete express.js 
Javascript :: API key header for appsync graphql request 
Javascript :: loop,array javascript 
Javascript :: javascript Implicit Conversion to Number 
Javascript :: eleventy filter newlines 
Javascript :: javascript Adding Properties And Methods in an Object 
Javascript :: javascript Symbol Properties 
Javascript :: matrix calculator in js 
Javascript :: javascript Error handling is easier to manage 
Javascript :: JavaScript Generator Function With return 
Javascript :: ajax introduction 
Javascript :: disable SerializableStateInvariantMiddleware and ImmutableStateInvariantMiddleware middlewares for large redux stores 
Javascript :: pizza form validation jquery 
Javascript :: get data notifacation realtime use ajax good 
Javascript :: ngswitch example on string 
Javascript :: vuejs.org español 
Javascript :: phaser animation repeat event 
Javascript :: How to call the API when the search value changes 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzqqqqqqqqqqqqqqqqqq 
Javascript :: react native bootsplash generate splash 
Javascript :: iterate over array of html elements 
Javascript :: Assigning All NodeLists A New Function 
Javascript :: how to declare a variable js 
Javascript :: javascript event.target 
Javascript :: vuejs 
Javascript :: javascript find json value 
Javascript :: react native 
Javascript :: javascript first class functions 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =