Ejemplo 1: javascript recursivo fibonacci
// Recursion way to return Fibonacci numbers
function recursiveFibonacci(n){
// base case
if(n === 0) return 0
if(n === 1) return 1
// recursioin base
return recursiveFibonacci(n - 2) + recursiveFibonacci(n - 1);
}
recursiveFibonacci(6) // should return 8
//////// and ////////
// Iteration way to return Fibonacci numbers
function iterativeFibonacci(n) {
if (n === 0) return 0;
if (n === 1) return 1;
let previous = 0;
let current = 1;
for (let i = n; i > 1; i--) {
let next = previous + current;
previous = current;
current = next;
}
return current;
}
iterativeFibonacci(6) // should return 8
Ejemplo 2: secuencia js fibonacci
// 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;
}
Ejemplo 3: secuencia js fibonacci
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]);
}
Ejemplo 4: función recursiva para series de fibonacci en 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));
Ejemplo 5: función recursiva para series de fibonacci en 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(2));
Ejemplo 6: función recursiva de JavaScript para series de fibonacci
Enter the number of terms: 12
0
1
1
2
3
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)