Fibonacci Series Algorithm

Published on 27 May, 2020

Question

Write a function fibonacci(n) that prints the first n fibonacci numbers.

Solution

function f(n) {
  if (n <= 2) {
    return 1;
  } else {
    return f(n - 1) + f(n - 2);
  }
}

function fibonacci(n) {
  for (var i = 1; i <= n; i++) {
    console.log(f(i));
  }
}