mardi 12 janvier 2021

Javascript Fibonacci Sequence generator not working properly [closed]

Can anyone explain why I keep getting nan instead of the next number in the fibonacci sequence? I've found others that work, but I need this to output as an array.

function fibonacciGenerator (n) {

    var output = [];
    if (n === 1) {
        output = [0];
    } 
    else if (n === 2) {
    output = [0, 1];
    } 
    else {
    output = [0, 1];

    for (var i = 2; i < n; i++) {
        output.push(output[output.lenght - 2] + output[output.lenght - 1]);
        }
    }
    return output;
}

output = fibonacciGenerator(4);

console.log(output)

Aucun commentaire:

Enregistrer un commentaire