Fibonacci

Revealing Module Pattern

by Ned Programming

HTML

<div id="result">

</div>

JavaScript

const fibonacciSequence = (() => {

	let arrayPlaceholder = null;
  let endValue = null;

	  
  function calculateSeq(firstVal, secondVal) {
  
    		arrayPlaceholder = new Array(firstVal, secondVal);
  			endValue = prompt("Enter end value: ");
  
  
        while(endValue < 0) {
        endValue = prompt("Negative numbers are not allowed, Please enter another value: ");
      }

      for(let i = arrayPlaceholder.length; i < endValue; i++) {
        arrayPlaceholder[i] = arrayPlaceholder[i - 2] + [i - 1];
      }

      return arrayPlaceholder;
  }
  
  return {
  	init: calculateSeq
  }
    
})();

const copycat = fibonacciSequence;

document.querySelector("#result").innerHTML = copycat.init(1,2);