Answers for "Generating N Fibonacci Series using Javascript"

3

how to generate a fibonacci sequence in javascript

// declare the array starting with the first 2 values of the fibonacci sequence
    let fibonacci = [0,1];
    
    function listFibonacci(num) {
    // starting at array index 1, and push current index + previous index to the array
        for (let i = 1; i < num; i++) {
            fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
        }
        console.log(fibonacci);
    }
    
    listFibonacci(10);
Posted by: Guest on July-13-2020
0

recursive function for fibonacci series in java javascript

var fib = function(n) {
  if (n === 1) {
    return [0, 1];
  } else {
    var arr = fib(n - 1);
    arr.push(arr[arr.length - 1] + arr[arr.length - 2]);
    return arr;
  }
};

console.log(fib(8));
Posted by: Guest on May-08-2020

Code answers related to "Generating N Fibonacci Series using Javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language