How to print the first N fibonacci numbers in JavaScript

1 Answer

0 votes
let n1 = 0, n2 = 1, n3, N = 10;    
  
console.log(n1);
console.log(n2);
     
for (let i = 2; i <N; i++) {    
    n3 = n1 + n2;    
    console.log(n3);    
    n1 = n2;    
    n2 = n3;    
}    
 
   
     
     
/*
run:
     
0
1
1
2
3
5
8
13
21
34
     
*/

 



answered Apr 3, 2021 by avibootz
edited Apr 3, 2021 by avibootz

Related questions

2 answers 137 views
1 answer 106 views
1 answer 216 views
1 answer 148 views
1 answer 141 views
1 answer 158 views
...