printing duplicate of last two number of fibonacci

Viewed 121

I am trying to print Fibonacci numbers up to n. The code below prints outs the last number twice for some reason, please help.

function fibonacci(n) {
  let i;
  const fib = [];

  fib[0] = 0;
  fib[1] = 1;

  for (i=2; i<n; i++) {
    fib[i] = fib[i-2] + fib[i-1];
    fib.push(fib[i]);
  }
  console.log( fib );
}
4 Answers
Related