I'm currently facing a challenge in creating a Fibonacci number generator and I've hit a roadblock. It seems like I have a solution, but the appearance of NaN's is causing me some trouble.
function fibonacciGenerator (n) {
var output = [];
if( n === 2 ){
output.push(0,1);
}
else if( n === 1 ){
output.push(0);
}
else{
output = [0,1];
while( n > output.length){
output.push((output[output.length - 2]) + (output[output.lenght - 1]));
}
}
return output
}
So, when I use the function with n=3 and higher, it adds the sum of the last two numbers in the output array to that array until n<output.length. Everything works as expected, the loop stops when n=output.lenght, but I keep getting back NaN's instead of numbers. What could I be doing incorrectly?