JSFiddle - React, Tailwind, and code Playground
by zazagalaxy
HTML
<html>
<head>
<center><h1>
Linear Interpolation
</h1></center>
<center><h2>
Assignment 11
</h2></center>
</head>
<body>
<br />
Enter a value between 0 and 6<br>Click Interpolate to determine the percentage of rainfall for the time chosen.
<input type="textbox" id="input" />
<br>
<input type="button" value="Interpolate" id="start" onClick='start()' />
<div class="container">
<div id="output">
</div>
</div>
<div id="output1">
</div>
</body>
</html>
CSS
.container {
width: 500px;
height: 50px;
margin: left;
text-align: left;
font-size: 18px;
border: 1px solid black;
word-wrap: break-word;
overflow-x: auto;
overflow-y: scroll;
padding: 5px;
}
#start {
background-color: maroon;
color: aquamarine;
margin-top: 10px;
margin-bottom: 10px;
width: 12em;
}
JavaScript
var list = new LinkedList();
var indexarray = [];
function LinkedList() {
this.length = 0;
this.head = null;
this.tail = null;
}
function Node() {
this.item = function(){
this.index = 0;
this.value = null;
}
this.next = null;
this.prev = null;
}
LinkedList.prototype.add = function(value) {
let node = new Node();
let i = 0;
node.item.index = this.length;
node.item.value = value;
if (this.head === null) {
this.head = node;
this.length = 1;
return node;
}
if (this.tail === null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length += 1;
return node;
}
LinkedList.prototype.dequeue = function(){
if (this.head == this.tail) {
var current = this.head;
this.head = null;
this.tail = null;
this.length = 0;
return current.item;
}
var previous = this.head;
this.head = this.head.next;
this.length -= 1;
return previous.item;
}
// Fill the linked list with six values.
function fill() {
list.add(0.00);
list.add(0.04);
list.add(0.11);
list.add(0.60);
list.add(0.87);
list.add(0.95);
list.add(1.00);
}
// Function to interpolate a value between two points.
function lerp(min, max, norm) {
// Reject values higher than 6.
if (max > 6) {
return "Please enter a value between 0 and 6.";
}
else if (norm < 0.50 && norm > 0.37) {
return 0.02;
}
else if (norm <= 0.37 && norm > 0.12){
return 0.01;
} else {
// Calculate the value between two points using linear interpolation.
var solution = (norm * (indexarray[max].value - indexarray[min].value) + indexarray[min].value).toFixed(2);
return solution;
}
}
// Fill an array with each node from the linked list.
function fillarray(){
var l = list.length;
var current = list.head;
for (var t = 0; t <= l; t++) {
indexarray.push(current.item);
...