Assignment 11

by Ebony McCoy

HTML

Enter a number between 0 and 6:
<input type="textbox" id="number" number="" />
<input type="button" id="button" value="Enter number" onClick="Interpolation()" />
<br/>
<br>
<p id="output"></p>

JavaScript

var table = [0.00, 0.04, 0.11, 0.60, 0.87, 0.95, 1.00];

function Interpolation() {
  clearDisplay();
  var c = parseFloat(document.getElementById("number").value);

  if (c < 0 || c > 6) {
    document.getElementById("output").innerHTML += "Please enter a number between 0 and 6.";
  } else if (c == 0 || c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6) {

    document.getElementById("output").innerHTML += "At hour " + c + ", " + table[c] + " of the total rain had fallen.";
  } else {
    var x = c;
    var x1 = Math.floor(x);
    var x2 = Math.ceil(c);
    var y1 = table[x1];
    var y2 = table[x2];
    var y = y1 + (y2 - y1) * ((x - x1) / (x2 - x1));
    var result = parseFloat(y).toFixed(2);
    document.getElementById("output").innerHTML = "At hour " + c + ", " + result + " of the total rain had fallen.";
  }
}

function clearDisplay() {
  document.getElementById("output").innerHTML = "";
}