Hello and welcome my name is Amulya and you are watching EaseCoding in this video i’m going to show you how to print fibonacci sequence in javascript so this is fibonacci sequence program that i’ve already written but i’m going to write it again and you can see i’ve given three arguments so basically three i’m trying to call the function three times with three different numbers and each time it prints the fibonacci sequence

so let’s get started i’m going to create a new file so save it

fabrics and now i’m going to write a function so it’s going to be a function

also going to name it hopefully you can see

fever and it’s going to take an argument or whatever number you give it so if you already know how fibonacci works then it’s fine otherwise this is how it works it starts with

two numbers and to print the next number it basically sums the last two digits in the sequence so it will become 0 plus 1 equal to 1 this time it will sum 1 plus 1 which are just two large digits and it will become free this time it will print two plus one and it will become three this time it will then two plus three which will become five and next number will become eight so that’s what we are going to achieve and this is repetitive task so we are going to use this function in the function we are going to use for loop so also to represent a sequence i’m going to use

um array and it is going to call two numbers first

we do not want to move on if the number given is one or two okay so we just want to print these numbers so i can simply say if n is less than or equal to one

it’s in javascript right

i want to console log

i’m going to console fq first number right similarly if it’s going to be 2

so i’m just going to put everything in just one line right if it’s going to be

two i’m going to change this number console console.log so this time i’m going to spread operator so it’s going to print one and two

okay

the third is going to be hopefully you can understand it

now if the number

if n is greater than two and we want to do certain things right we want to take the last two digit and it add to uh as a next number so if it’s

more than two we are going to give the for loop so this is for loop i’m going to use that i equal to 1 i is less than or equal to

n minus 2 because we already have n here and there are two

values in it so i’m going to just deduct those two values and i want to n plus plus sorry i plus plus now here

this is going to the loop is going to run for n minus 2 times right and each time we want to fq push so we want to take the last two digi two digit from two numbers from the array and keep pushing so fq

plus fq and here we are going to take last number and last second last number so to do so we simply say fqln minus 1.

similarly we’re going to say here minus 2 so each time it’s going to take those numbers last two digits right

and we are done now we want to do console log

and using this thread operator i can simply print this number

now let’s go ahead and run this code i’m going to use this fiba a few times at the bottom so basically let’s put one

this time two

this time ten

okay so let me go ahead and run the code i’m going to save the file hopefully you can see it clearly now so this is the code we have here and we are calling this function three times with three different numbers now i’m going to run this code in my command prompt with node.js so node paper

and you can see it’s doing the same thing as above so for given number one it’s going to print the first number only given number two is going to print two numbers that are already given to fq array and then 10 times 2 4 6 8 10 so it’s working perfectly fine so this is how you can print Fibonacci sequence number for a given number in JavaScript that’s it for this video guys thank you for watching bye for now