generate fibonacci sequence
up to n terms : print to console
[ while-loop ]

Generate fibonacci sequence up to n terms

Write a function to generate a fibonacci sequence up to n terms. Print the sequence to console.


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 prints to console the first 7 terms of the Fibonacci sequence.

0

1

1

2

3

5 → print to console

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 a while loop


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

}

let x = 0; → counter

while (x < 4) {

console.log(x);

x++;

}

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 printed to the console.

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 → printed to console


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 first two numbers of the sequence.

let num1 = 0 num1 First Fibonacci number

let num2 = 1 num2 Second Fibonacci number

Initialize a variable to hold the next Fibonacci number.

let nextNum = 0 nextNum

Start the while loop from index 2

The first two terms have already been defined.

let x = 2 x Counter to track the number of terms printed

Print the header for the Fibonacci sequence.

console.log("Fibonacci Sequence:")

Handle the case when only the first term is requested.

if (num == 1) {}

Return 0 for the first term and end execution of function.

return 0

Handle the case when only the first two terms are requested.

if (num == 2) {}

Print the first term.

console.log(0)

Return 1 for the second term and end execution of function.

return 1

For case where more than two terms are requested.

Print the first two terms - which are already defined.

console.log(num1) 0

console.log(num2) 1

Loop to calculate the next terms.

while (x < num) {}

Calculate the next term nextNum by adding the two previous numbers in the array.

nextNum = num1 + num2

Print nextNum

console.log(nextNum)

Update num1 to the previous num2

num1 = num2

Update num2 to the newly calculated nextNum

num2 = nextNum

Increment the counter by 1

x++

When the loop while (x < num) evaluates to false, the loop terminates.


Call the function with ↴

fibonacci(nTerms);


Generate the first 7 terms of the Fibonacci sequence.

num = 7

The first two terms have already been defined.

num1 = 0 first number   0

num2 = 1 second number   1

nextNum is computed by adding the two previous numbers.

nextNum = num1 + num2

let x = 2

while (x < num)

x Iteration ↴

2 num1 = 0, num2 = 1 0 + 1 1

3 num1 = 1, num2 = 1 1 + 1 2

4 num1 = 1, num2 = 2 1 + 2 3

5 num1 = 2, num2 = 3 2 + 3 5

6 num1 = 3, num2 = 5 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 num1 = 0;

let num2 = 1;

let nextNum = 0;

let x = 2;

console.log("Fibonacci Sequence:");

if (num == 1) {

return 0;

}

if (num == 2) {

console.log(0);

return 1;

}

console.log(num1);

console.log(num2);

while (x < num) {

nextNum = num1 + num2;

console.log(nextNum);

num1 = num2;

num2 = nextNum;

x++;

}

}

call function

fibonacci(nTerms); returns ↴

Fibonacci Sequence:

0

1

1

2

3

5

8 → printed to console

Generate Fibonacci sequence

Enter number of terms to generate