Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

fibonacci sequence javascript

function fibonacciGenerator (n) {
  var outPut = [];
  if (n === 1){
    outPut = [0];
  }
  else if (n === 2){
    outPut = [0,1];
  }else{
    outPut = [0,1];
    for(var i = 2; i < n; i++){
      outPut.push(outPut[outPut.length - 2] + outPut[outPut.length - 1])
    }    
  }//Return an array of fibonacci numbers starting from 0.
    console.log(outPut)
    return outPut
}
fibonacciGenerator(8);
Source by replit.com #
 
PREVIOUS NEXT
Tagged: #fibonacci #sequence #javascript
ADD COMMENT
Topic
Name
6+4 =