Why does adding two array lengths sum their index values instead of summing value of their actual length?

Viewed 64

I can see that the function is adding output[19] to output[18] to generate a sum of 4181, however why is the sum not 37?

function fibonacciGenerator(n) {

  var output = [];

  for (var i = 0; i < n; i++) {
    if (i < 2) {
      output.push(i)
    } else {
      output.push(output[output.length - 1] + output[output.length - 2])
    }
  }
  console.log(output);
}

fibonacciGenerator(20);

0 Answers
Related