generate fibonacci sequence
up to n terms : print to console
[ for-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

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 for loop


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

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 Fibonacci numbers 0 and 1

let num1 = 0 num1 first number

let num2 = 1 num2 second number

Initialize a variable to hold the next number in the sequence.

let nextNum nextNum

Print the header for the Fibonacci sequence.

console.log("Fibonacci Sequence:")

Loop to generate the Fibonacci sequence.

for (let x = 1; x <= num; x++) {}

Print each Fibonacci number as it iterates through loop.

console.log(num1)

Calculate the next number in the sequence,

sum of the last two numbers.

nextNum = num1 + num2 nextNum

Update num1 to the second number num2

num1 = num2

Update num2 to the newly calculated number.

num2 = nextNum

Loop continues until the condition evaluates to false, terminating the loop.


Call the function with ↴

fibonacci(nTerms);


Generate the first 7 terms of the Fibonacci sequence.

num = 7

Loop to calculate the Fibonacci numbers.

for (let x = 1; x <= num; x++) {}

x = 1

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

num1 + num2

x Iteration ↴

1 num1 = 0 first number → 0

2 num2 = 1 second number → 1

3 num1 = 0, num2 = 1 0 + 11

4 num1 = 1, num2 = 1 1 + 12

5 num1 = 1, num2 = 2 1 + 23

6 num1 = 2, num2 = 3 2 + 35

7 num1 = 3, num2 = 5 3 + 58

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;

console.log("Fibonacci Sequence:");

for (let x = 1; x <= num; x++) {

console.log(num1);

nextNum = num1 + num2;

num1 = num2;

num2 = nextNum;

}

}

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