1.循环法
function fel(n) {
if (n === 1 || n === 2) {
return 1;
}
var n1 = 1;
var n2 = 1;
var fiba;
for (var i =3;i<=n;i++) {
fiba = n1+n2;
n1 = n2;
n2 =fiba;
}
console.log(fiba)
return fiba;
}
fel(8)
2.递归法
function fiba(n) {
if (n === 1 || n===2) {
return 1;
}else {
return fiba(n-1) + fiba(n-2);
}
}
来源:CSDN
作者:tmacer
链接:https://blog.csdn.net/tmacer/article/details/104426922