JSFiddle - React, Tailwind, and code Playground

by hesster92

HTML

Assignment 11<br><br> Enter a number between 0 and 6
<br><br>
<input type="textbox" id="hour">
<br><br>
<input type="button" id="btnHour" value="Enter a number" onClick="calcRainfall()">
<br><br>
<p id="output">

</p>

JavaScript

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

function calcRainfall() {
  clearDisplay();
  var j = parseFloat(document.getElementById("hour").value);
  if (j < 0 || j > 6) {
    document.getElementById("output").innerHTML += "Enter a number between 0 and 6, You entered " + j;
  } else if (j == 0 || j == 1 || j == 2 || j == 3 || j == 4 || j == 5 || j == 6) {

    document.getElementById("output").innerHTML += j + " = " + rainfall[j] + " of the total rain had fallen.";
  } else {
    var x1 = Math.floor(j);
    var x2 = Math.floor(j + 1);
    var y1 = rainfall[Math.floor(j)];
    var y2 = rainfall[Math.floor(j + 1)];
    var y = (((j - x1) * (y2 - y1)) / (x2 - x1)) + y1;
    var totalRainfall = parseFloat(y).toFixed(2);

    document.getElementById("output").innerHTML = j + " = " + totalRainfall + " of the total rain had fallen.";
  }
}

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