//fibonacciNumbers example = [0,1,1,2,3,5,8,13,21,34,55....]
var fibonacciNumbers = [];
var numb1 = 0;
var numb2 = 1;
var sum;
do{
console.log(numb1); //0 //1 //1 // 2 // 3 ......
sum = numb1 + numb2; // 0+1 //1+1=2 //1+2 =3 // 2+3 =5 // 3 + 5 = 8 ......
numb1 = numb2; // 0 = 1 //1 = 1 //1 = 2 // 2 = 3 // 3 = 5 ......
numb2 = sum; // 1 = 1 //1+1=2 // 1+2 =3 // 2+3 =5 // 3 + 5 = 8 ......
}while(numb1<=20);
for(i=0; i<=5; i++){
fibonacciNumbers.push(numb1);
sum = numb1 + numb2;
numb1 = numb2;
numb2 = sum;
}
console.log(fibonacciNumbers);
here is my console log result in node (vs code editor)
0
1
1
2
3
5
8
13
[ 21, 34, 55, 89, 144, 233 ]
It is correct but what will be the length of fibonacci array. Is it n number i.e. upto infinity or specific number given by us/user i.e. 20 or 200 or more.
<20 <of first one, second one is only print 5 number , but it exicuted 6 time
Actually this logic and my logic both will work perfectly upto 78 but from 79 it gives us wrong answer so I’m trying to solve it. You can also try to solve this problem and let me know if you solve this problem.
fibonacci number
Here its example
it's calculation of last two number -fibonacci number
0,1,1,2,3,5,8,...
if you want start with 0 or second number is 1 ,
// first two value is your given value as it as
first is 0 then second is 1,
next is 0+1 =1,
next is 1+1=2,
next is 1+2=3,
next is 2+3=5,
//result
[0,1,1,2,3,5,8....]
*****************************
do not put first number is greater then second -- it will work but , here I explain it.
if you want start with 10 or second number is 1 ,
// first two value is your given value as it as
first is 10 then second is 1,
next is 10+1=11,
next is 1+11=12,
next is 11+12=23,
//result
[10,1,11,12,23,35...]
**checkout this code live on CodeSandbox . I put 10000 value in first condition , code is working well, no found issue in this code **
using closer function
//Closer Function-- function alonge with it's lexical scope bundle together form the close -that is a closer
let result =[], value = 10 ;
function start(){
let x = 0;
function nextvalue(){
let y = 1;
function sum(){
for(let z=0; z<value; z++){
result.push(x);
let z = x+y;
x = y;
y = z;
}
}
sum();
}
nextvalue();
}
start();
console.log(result);
/*complex one*/
let result2 = [];
function startNew(a, value){
let x = a;
return function nextvalue(b){
let y = b;
return function sum(){
for(let z=0; z<value; z++){
result2.push(x);
let z = x+y;
x = y;
y = z;
}
}
}
}
let nextvalue = startNew(0,5);
let sum = nextvalue(1);
sum();
console.log(result2);