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

recursion → programming technique where a function calls itself repeatedly to solve a problem.

for loop → executes a block of code a number of times.


Recursion The act of a function calling itself.

Recursion is used to solve problems that contain smaller sub-problems.

A recursive function can receive two inputs: a base case (ends recursion) or a recursive case (resumes recursion).

Use recursion to find the factorial of 5.

let x = 5;

function factorial(num) {

if (num > 1) { → Recursive call

return num * factorial(num - 1);

}

else { → Base case

return 1;

};

}

call function

factorial(x); returns ↴

120 factorial of 5 → 120


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(nTerms) {}

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

Base case

nTerms if it is less than 2

if (nTerms < 2) {}

Return nTerms and end execution of function.

return nTerms

Recursive case

Function calls itself twice to compute the sum of the two preceding Fibonacci numbers.

return fibonacci(nTerms - 1) + fibonacci(nTerms - 2)

Print the header for the Fibonacci sequence.

console.log("Fibonacci Sequence:")

Initialize variable to store the nth Fibonacci term.

let nthTerm nthTerm

Loop through the range of terms and calculate Fibonacci numbers.

for (let x = 0; x < nTerms; x++) {}

Calculate the Fibonacci number for the current index x by calling the fibonacci function.

let fibResult = fibonacci(x) fibResult

Print the result fibResult to the console.

console.log(fibResult)

Store the last calculated Fibonacci number.

nthTerm = fibResult


Call the function with ↴

fibonacci(nTerms);


Generate the first 7 terms of the Fibonacci sequence ↴

function fib(nTerms)

nTerms = 7

for (let x = 0; x < nTerms; x++) {}

Calculate the Fibonacci number for the current index x by calling the fibonacci function.

fibonacci(x)

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

return fib(nTerms - 1) + fib(nTerms - 2)

x Iteration ↴

0 fib(0) fib(-1) + fib(-2) 0 Base case

1 fib(1) fib(0) + fib(-1) 1 Base case

2 fib(2) fib(1) + fib(0) 1

3 fib(3) fib(2) + fib(1) 2

4 fib(4) fib(3) + fib(2) 3

5 fib(5) fib(4) + fib(3) 5

6 fib(6) fib(5) + fib(4) 8

The function stacks calls, until it reaches the base case, then unwinds the stack to produce the result ↴

0, 1, 1, 2, 3, 5, 8


Generate the first 7 terms of the Fibonacci sequence.

const nTerms = 7;

function fibonacci(nTerms) {

if (nTerms < 2) {

return nTerms;

} else {

return fibonacci(nTerms - 1) + fibonacci(nTerms - 2);

}

} end of function

console.log("Fibonacci Sequence:");

let nthTerm;

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

let fibResult = fibonacci(x);

console.log(fibResult);

nthTerm = fibResult;

}

returns ↴

Fibonacci Sequence:

0

1

1

2

3

5

8 → printed to console

Generate Fibonacci sequence

Enter number of terms to generate