Javascript Algorithm Number Nth fibonacci number via recursive function

Description

Javascript Algorithm Number Nth fibonacci number via recursive function

function getNthFiboBetter(n, lastlast, last) {
    /*from w w w. ja va 2  s  .  c om*/
    if (n == 0) {
        return lastlast;
    }
    if (n == 1) {
        return last;
    }
    
    return getNthFiboBetter(n-1, last, lastlast + last);
}
for (var i=1; i < 10; i++) {
    console.log(getNthFiboBetter(i,0,1));
}



PreviousNext

Related