//Assignment 11
var rainfall = [0.00, 0.04, 0.11, 0.60, 0.87, 0.95, 1.00];
//The enter key handler
function handle(e) {
var key = e.keyCode || e.which;
if (key == 13) {
input = document.getElementById("input").value;
findRainfall(rainfall, input);
}
}
function findRainfall(rainfall, input) {
input = document.getElementById("input").value;
limitInput(input);
document.getElementById("output").innerHTML = input + " = " + interpol(rainfall, input);
document.getElementById("input").value = "";
}
//Limit user input
function limitInput(input) {
x = document.getElementById("input").value;
if (isNaN(x) || x < 0 || x > 6) {
alert("Please enter only a number between 0 and " + (rainfall.length - 1) + ". You entered: " + input);
}
}
function interpol(rainfall, input) {
var number = parseFloat(input);
var x = number; // Target X co-ordinate
var x1 = Math.floor(number); // X1 = First co-ordinates
var x2 = Math.floor(number + 1); // X2 = Second co-ordinates
var y1 = rainfall[Math.floor(number)]; // Y1 = First co-ordinates
var y2 = rainfall[Math.floor(number + 1)]; // Y2 = Second co-ordinates
// Y = Interpolated Y co-ordinate.
var y = (((x - x1) * (y2 - y1)) / (x2 - x1)) + y1;
return number == rainfall.length - 1 ? rainfall[number] : Number(y).toFixed(2);
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.