Answers for "fibonacci sequence iterative javascro[t"

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

Code answers related to "Javascript"

Browse Popular Code Answers by Language