jakiro

JS fibonacci (For loop + if else + arr)

Apr 14th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fibonacciGenerator (n) {
  2. //Do NOT change any of the code above 👆
  3.  
  4.     //Write your code here:
  5.     if (n===1){
  6.         return [0];
  7.     }
  8.     if (n===2){
  9.         return [0,1];
  10.     }
  11.    
  12.     var arr = [0,1];
  13.    
  14.     for(var i=2; i<n ; i++){
  15.         arr.push(arr[i-1]+arr[i-2]);
  16.     }
  17.        
  18.     return arr;
  19.    
  20.     //Return an array of fibonacci numbers starting from 0.
  21.    
  22. //Do NOT change any of the code below 👇
  23. }
Advertisement
Add Comment
Please, Sign In to add comment