Skip to content

Commit 20d3b93

Browse files
committed
[added]: Answer to ch3 readme
1 parent 40051e8 commit 20d3b93

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

solutions/ch_3_Fibonacci_Series/readme.md

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
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]`.
44

5+
## Answer
6+
7+
```javascript
8+
function fibonacci(n) {
9+
// Create an array to store the Fibonacci sequence
10+
const fib = [0, 1];
11+
12+
// Generate the next number in the sequence by adding the two previous numbers
13+
for (let i = 2; i < n; i++) {
14+
fib[i] = fib[i - 1] + fib[i - 2];
15+
}
16+
17+
// Return the first n numbers in the sequence
18+
return fib.slice(0, n);
19+
}
20+
```
21+
522
## Answer Explanation
623

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

0 commit comments

Comments
 (0)