recursion/fibonacci.js (via loops)

In this example below you will see how to do a recursion/fibonacci.js (via loops) with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by alexsales, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright alexsales ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>recursion/fibonacci.js (via loops)</title>
  
  
  
  
  
</head>

<body>

  <div class="fibonacciArray"></div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/alexsales/recursionfibonaccijs-via-loops-avxXNL */
var fullFibArray = [];
var nArray = [];
var fibArray = [0, 1, 1];

function setView(_obj) {
  document.getElementsByClassName('fibonacciArray')[0].innerHTML += _obj.n + ', ' + _obj.fib + '<br/>';
}

function combineArrays(_n) {
  var obj = {};

  for (var i = 0; i <= _n; i++) {
    obj.n = i;
    obj.fib = fibArray[i];
    fullFibArray.push(obj);
    
    setView(obj);
  }
  console.log(fullFibArray);
}

function fillFibArray(_n) {
  for (var i = 0; i <= _n; i++) {
    var sum = 0;

    /*console.log(fibArray[i]);*/
    sum = fibArray[i] + fibArray[i + 1];
    fibArray[i + 2] = sum;
  }
  console.log(fibArray);
}

function fillNArray(_n) {
  for (var i = _n; i >= 0; i--) {
    nArray.unshift(i);
  }
  console.log(nArray);
}

fillNArray(10);
fillFibArray(10);
combineArrays(10);

/*console.log(nArray.length);*/

Comments