Skip to content

Home

Fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

const fibonacci = n =>
  Array.from({ length: n }).reduce(
    (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
    []
  );

fibonacci(6); // [0, 1, 1, 2, 3, 5]

More like this

Start typing a keyphrase to see matching snippets.