js-coding-challenges

Challenge 3: Fibonacci Series

Write a function that takes a number n as input and returns the first n numbers in the Fibonacci sequence. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. The first two numbers in the sequence are 0 and 1. For example, if n is 5, the function should return the array [0, 1, 1, 2, 3].

Answer

function fibonacci(n) {
  // Create an array to store the Fibonacci sequence
  const fib = [0, 1];

  // Generate the next number in the sequence by adding the two previous numbers
  for (let i = 2; i < n; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
  }

  // Return the first n numbers in the sequence
  return fib.slice(0, n);
}

Answer Explanation

The function takes a single argument, n, which is the number of Fibonacci numbers to generate. Here’s what the function does:

Here’s an example usage of the function:

console.log(fibonacci(5)); // [0, 1, 1, 2, 3]
console.log(fibonacci(10)); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

In this example, the function correctly generates the first 5 and 10 numbers in the Fibonacci sequence, respectively.