Answers for "calculate fibonacci number in 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

js to confirm fibonnaci

function isFibonacci(n) {
  var fib,
    a = (5 * Math.pow(n, 2) + 4),
    b = (5 * Math.pow(n, 2) - 4)

  var result = Math.sqrt(a) % 1 == 0,
    res = Math.sqrt(b) % 1 == 0;

  //fixed this line
  if (result || res == true) // checks the given input is fibonacci series
  {
    fib = Math.round(n * 1.618); // finds the next fibonacci series of given input
    console.log("The next Fibonacci number is " + fib);

  } else {
    console.log(`The given number ${n} is not a fibonacci number`);
  }
}

$('#fib').on("keyup change", function() {
  isFibonacci(+this.value)
})
Posted by: Guest on July-04-2020

Code answers related to "calculate fibonacci number in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language