Fibonacci calculator

Show me the

th place in the Fibonacci sequence



Behind the scenes

I tried to calculate the Fibonacci sequence by hand once, and a single error can put you back to the beginning because I wasn’t exactly sure where I made the error and let me tell you, I lost an hour to that.

When you do it with JavaScript it decreases the amount of errors, I can’t say for sure that there is none, but I can say that there would be far less than doing it by hand. By using HTML/Javascript I was able to calculate the numbers to a higher degree, quicker.

It feels so natural – the higher it gets, the more obvious the changes are. The hundredth number is more than one billion times the 2nd number in the sequence – 1. The math of calculating the 100th term of the Fibonacci sequence is much more accurate and time efficient than doing it by hand. 

I had to add a validation function using an if and an else code to limit the size of the number that the user can enter as it can cause issues if the number is too big, and an elseif code if they enter "1". Below is the code used to create the device.

The code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
  </head>
  <body>
<p>
Show me the
</p>
<!--this is where the user inputs what place in the Fibonacci sequence they'd like it to go to-->
<p><form><input type="number" id="uPlaces"> th place in the Fibonacci sequence
<br>
<button type="button" onclick="fibonacci()">calculate</button></p>
<script type="text/javascript"> 
function fibonacci(){
var Places=document.getElementById('uPlaces').value;
let fn=1 // declare variable "fn" as value=1
let pfn=0 //declare variable "pfn" as equal to fn (for now)
if (Places>1476){document.getElementById("fibanswer").innerHTML=`WHOA NELLY that is tooooooo big!!!!!!!`
}else if (Places==1){document.getElementById("fibanswer").innerHTML="1"
}else{
for (let i=1; i<=Places-1;i++){ // as long as the number of loops is less than Places-2, keep iterating the loop
    fn+=pfn; //increase fn by pfn
    pfn=fn-pfn;//set pfn to equal the value of the variable fn minus the value of the variable pfn
document.getElementById("fibanswer").innerHTML=fn
}
}
return;
}
          </script>
<!--this piece of code is used to visualize the output-->
          <br><p id="fibanswer"></p>
  </body>
</html>