fibonacci

by ellhn

HTML

<html>
<head>
<body>
  <input width="50" id="fibo"/>
  <div>
    <button type="button" id="reset">
       RESET
    </button>
    <button type="button" id="next">
       NEXT
    </button>
  </div>
</body>
</head>
</html>

JavaScript

function fibonacci(reset){
  if (!fibonacci.currentValue || reset) {
    fibonacci.previousValue = 0;
    return fibonacci.currentValue = 1;
  } 
  var temp = fibonacci.previousValue;
  fibonacci.previousValue = fibonacci.currentValue;
  return fibonacci.currentValue += temp;
}

function resetInput(){
  fibo.value = fibonacci(true);
}
function nextInput(){
  fibo.value = fibonacci();
}

var fibo = document.getElementById("fibo");
var reset = document.getElementById("reset");
var next = document.getElementById("next");
reset.addEventListener("click", resetInput);
next.addEventListener("click", nextInput);