Javascript Algorithm Number Nth fibonacci number

Description

Javascript Algorithm Number Nth fibonacci number

function getNthFibo(n) {
    if (n <= 1) return n;

    var sum = 0,//from ww w .j a v a2 s .  c  o  m
        last = 1,
        lastlast = 0;

    for (var i = 1; i < n; i++) {
        sum = lastlast + last;
        lastlast = last;
        last = sum;
    }
    return sum;
}
for (var i=1; i < 10; i++) {
    console.log(getNthFibo(i));
}



PreviousNext

Related