Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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]
Comment

javascript fibonacci example

// number fibonnaci to array format
function fibonacci(nums) {
  
  let fib = [0, 1];
  let data = [];
  
  for(let i = 2; i <= nums; i++) {
    fib[i] = fib[i - 1] + fib[i - 2]; 
    data.push(fib[i]);
  }
  
  return data;
}
Comment

js fibonacci sequence

var i;
    var fib = []; // Initialize array!

    fib[0] = 0;
    fib[1] = 1;
    for (i = 2; i <= 10; i++) {
      // Next fibonacci number = previous + one before previous
      // Translated to JavaScript:
      fib[i] = fib[i - 2] + fib[i - 1];
      console.log(fib[i]);
    }
Comment

javascript 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

fibonacci sequence javascript

function fibonacciGenerator (n) {
  var outPut = [];
  if (n === 1){
    outPut = [0];
  }
  else if (n === 2){
    outPut = [0,1];
  }else{
    outPut = [0,1];
    for(var i = 2; i < n; i++){
      outPut.push(outPut[outPut.length - 2] + outPut[outPut.length - 1])
    }    
  }//Return an array of fibonacci numbers starting from 0.
    console.log(outPut)
    return outPut
}
fibonacciGenerator(8);
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 :: javascript random 1 or 0 
Javascript :: convert a string to array in javascript 
Javascript :: node-fetch 
Javascript :: bodyparser express deprecated 
Javascript :: javascript check if number 
Javascript :: constant values javascript 
Javascript :: how to write a comment in react js 
Javascript :: match string in array javascript 
Javascript :: How to block ctrl+shift+j using javascript 
Javascript :: js get integer value of 
Javascript :: for of loop 
Javascript :: javascript change all text color 
Javascript :: tostring() javascript 
Javascript :: javascript get width 
Javascript :: javascript calculate average of array 
Javascript :: sending value in input angular material 
Javascript :: api testing app with websocket 
Javascript :: how to create node js server 
Javascript :: mongodb text search 
Javascript :: How to make remove buttoon on table using js DOM 
Javascript :: jquery cdn by google 
Javascript :: datatables cdn 
Javascript :: trigger a function inside child from parent vue 
Javascript :: every break js 
Javascript :: console log error javascript 
Javascript :: call apply and bind method in javascript 
Javascript :: jsx loop array 
Javascript :: get all matches regex javascript 
Javascript :: Regex Chords 
Javascript :: js map size 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =