Answers for "fibonacci js"

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
5

fibonacci javascript

function Fibonacci(num){
	var before = 0;
	var actual = 1;
	var next = 1;

	for(let i = 0; i < num; i++){
		console.log(next)
		before = actual + next;
		actual = next
		next = before
	}
}

Fibonacci(100);
Posted by: Guest on December-14-2020
3

fibonacci javascript

function Fibonacci(valor){
	var anterior = 0;
	var atual = 1;
	var proximo = 1;

	for(let i = 0; i < valor; i++){
		console.log(proximo)
		anterior = atual + proximo;
		atual = proximo
		proximo = anterior
	}
}

Fibonacci(100);
Posted by: Guest on December-14-2020
0

fibonacci best performance javascript

// A sequência começa com 0 e 1, depois,
// os números subsequentes são compostos
// pela soma dos dois números anteriores
// da sequência. Daí:
// (x - 2) + (x - 1) = próximo número da sequência
function fibonacci(n) {
	const list = [0, 1];
	for (let x = 2; x < n + 1; x += 1) {
		list.push(list[x - 2] + list[x - 1]);
	}
	return list[n];
}

console.log(fibonacci(4));
Posted by: Guest on May-28-2020
1

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]);
    }
Posted by: Guest on July-03-2020
0

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;
}
Posted by: Guest on June-16-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language