Fibonacci sequence array
Medium
8
59.1% Acceptance
Write a function getFibonacci(n)
, which takes an integer n
as an input. This function should return the array containing the first n numbers in the Fibonacci sequence.
The Fibonacci sequence is a series of numbers where each number is the sum of the previous two numbers, starting with 0 and 1. It is a famous mathematical concept that appears in nature, art, and many other fields. The sequence has a unique mathematical property that makes it useful in many applications, including computer algorithms, finance, and cryptography.
Instructions
- You can assume that the input
n
will always be a positive integer.
Example test cases
getFibonacci(1); // Output: [0] getFibonacci(2); // Output: [0, 1] getFibonacci(5); // Output: [0, 1, 1, 2, 3] getFibonacci(10); // Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Hints
- You should give special treatment for cases where
n
is equal to 1 or 2. - For cases when
n
is greater than 2, write a for loop that builds the output array.