generate fibonacci sequence
up to n terms : return an array
[ while-loop | slice ]

Generate fibonacci sequence up to n terms

Write a function to generate a fibonacci sequence up to n terms. Return the sequence as an array.


Fibonacci sequence

The Fibonacci sequence is the series of numbers where each number is the sum of the two preceding numbers.

The sequence typically starts with 0 and 1, and then the next term is defined as the sum of the previous two terms.

For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, ... and so on.

Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers.

A Fibonacci sequence can be generated for any number of terms.

Fn = Fn - 1 + Fn - 2 where F0 = 0 and F1 = 1


Example ...

Enter number of terms to generate ...

7 → number

The function generates the first 7 terms of the Fibonacci sequence, and returns an array.

[0, 1, 1, 2, 3, 5, 8]

Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Generate a fibonacci sequence up to n terms using ↴

while loop → repeatedly executes a block of code as long as a specified condition is true.

slice() method → returns selected elements in an array, as a new array.


while loop repeatedly executes a block of code as long as a specified condition evaluates to true.

while (condition) {

// execute code as long as condition is true

}

const arr = []; → empty array

let x = 0; → counter

while (x < 4) {

arr.push(x);

x++;

}

console.log(arr);

Initialize an empty array arr outside of the loop.

Initialize a counter variable x outside of the loop.

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is pushed to the end of the array.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

[0, 1, 2, 3] → array returned


slice() method returns selected elements in an array, as a new array.

syntax ↴

slice(start) return a new array from start index to end of array

slice(start, end) return an array from start index to end index of array (exclusive).

Return a new array from index 1 to end of array.

const arr2 = [1, 2, 3, 4];

arr2.slice(1); start index is 1

returns ↴

[2, 3, 4]

Return a new array from index 1 to index 4 (exclusive).

const arr3 = [1, 2, 3, 4];

arr3.slice(1, 3); start index is 1 end index is 4 (not included)

returns ↴

[2, 3]


Initialize a variable to hold number of terms n to generate for a Fibonacci sequence.

const nTerms = n; → user input


Define a function fibonacci() to generate a Fibonacci sequence.

function fibonacci(num) {}

The function takes a number as input num and generates first n terms of the Fibonacci sequence.

Initialize the array with the first two Fibonacci numbers 0 and 1

let fib = [0, 1] fib

Start from the third term.

let x = 2 x

Loop while x is less than num

while (x < num) {}

Each term is the sum of the two preceding terms.

fib[x] = fib[x - 1] + fib[x - 2]

Increment the index by 1

x++

Return the array fib sliced to the number of terms num requested.

return fib.slice(0, num)


Call the function with ↴

fibonacci(nTerms);


Generate the first 7 terms of the Fibonacci sequence.

The current Fibonacci number is computed by adding the two previous numbers in the array.

fib = [0, 1] array initialized with the first two numbers of the series.

fib[0] first number   0 [0, 1]

fib[1] second number   1 [0, 1]

Loop to calculate the Fibonacci numbers from the 3rd term onward.

let x = 2

while (x < num) {}

Add number at index [x - 1] to number at index [x - 2]

fib[x] = fib[x - 1] + fib[x - 2]

x Iteration ↴

2 fib[2] = fib[1] + fib[0] 1 + 0 1 [0, 1, 1]

3 fib[3] = fib[2] + fib[1] 1 + 1 2 [0, 1, 1, 2]

4 fib[4] = fib[3] + fib[2] 2 + 1 3 [0, 1, 1, 2, 3]

5 fib[5] = fib[4] + fib[3] 3 + 2 5 [0, 1, 1, 2, 3, 5]

6 fib[6] = fib[5] + fib[4] 5 + 3 8 [0, 1, 1, 2, 3, 5, 8]

First 7 terms of the Fibonacci sequence ↴

[0, 1, 1, 2, 3, 5, 8]


Generate the first 7 terms of the Fibonacci sequence.

const nTerms = 7;

function fibonacci(num) {

let fib = [0, 1];

let x = 2;

while (x < num) {

fib[x] = fib[x - 1] + fib[x - 2];

x++;

}

return fib.slice(0, num);

}

call function

fibonacci(nTerms); returns ↴

[0, 1, 1, 2, 3, 5, 8]

Generate Fibonacci sequence

Enter number of terms to generate