Fibonacci Sequence

Generates a Fibonacci sequence of a given length.

Contributed by @itsbrunodev

javascript
function fibonacci(n) {
  const seq = [0, 1];
 
  for (let i = 2; i < n; i++) {
    seq.push(seq[i - 1] + seq[i - 2]);
  }
 
  return seq.slice(0, n);
}
javascript
fibonacci(10); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Keywords

math
fibonacci