// 循环
function fibonacci_for(num) {
let l = [];
let a = 0;
let b = 1;
for (let i = 0; i < num; i++) {
l.push(a);
const tmp = a;
a = b;
b = tmp + b;
}
return l;
}
// 递归
function fibonacci_recursive(num) {
let l = [];
function fibonacci(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
for (let i = 0; i < num; i++) {
l.push(fibonacci(i));
}
return l;
}
res = fibonacci_recursive(10);
console.log(res);