Interpolation Assignment11
by arosado417
HTML
<h1 id="title">Assignment 11 / Interpolation</h1>
<label for="data">Enter a number in the range 0-6:</label>
<input type ="textbox" id="data" placeholder="Text Input" />
<input type ="button" value="Submit" onclick="startCalc()" />
<h3 id="output"></h3>
CSS
label {
margin-top: 10px;
}
label,
textarea,
select {
width: 60%;
display: block;
font-size: 20px;
}
JavaScript
var answer= "";//create a global var
//create an array
var table = [];
//add variables
table.push(0.00);
table.push(0.04);
table.push(0.11);
table.push(0.60);
table.push(0.87);
table.push(0.95);
table.push(1.00);
//create an array function interPolate to handle the arithmetic(interpolation)
Array.prototype.interPolate = function(value){
var hour = document.getElementById("data").value;
var i = value;
var total = ((table[i+1]- table[i]) *(hour - i)) + table[i];
answer = total.toFixed(2);
return answer;
}
//create function startCalc for boolean functioning
function startCalc(){
var hour = document.getElementById("data").value;
//creating multiple or statements to warn user of invalid value input
if(hour.length == 0 || (hour < 0 || hour > 6)){
console.log("Please try again");
document.getElementById("data").value ="Please try again";
}
//setting calc to false... code will not implement until calc is set to true within this block
//if the text input is in the table this block will implement
var calc = false;
for(var i = 0; i <= 6; i++ ){
if(i == hour){
calc = true;
answer = table[i];// no interpolation needed
console.log(table[i]);
var rainTotal = document.getElementById("output");
rainTotal.innerHTML= "The total rain fall in the hour is " + answer;
break;
}
}
//if last block remains false this block will implement
//if the text input is not in the table this block will implement
if(calc == false){
for(i = 0; i <= 6; i++ ){
if(hour > i && hour < (i+1)){
table.interPolate(i);
break;
}
}
//print statement for interpolation values
var rainInt = document.getElementById("output");
rainInt.innerHTML = "The total rain fall in the hour is " + answer;
}
}
/*
ASSIGNMENT 11
Assignment 11 – Interpolation
Objective
Develop and implement a basic algorithm to perform a simple mathematical function using an array
Supports learning objective 3
3. Implement data structures and algorithms in computer code.
Introduction
Just like many of the other data structures covered in this...