-
Notifications
You must be signed in to change notification settings - Fork 1
/
matt.js
195 lines (156 loc) · 4.97 KB
/
matt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//Desired parameters - pick 2 of 3, get answer for third
var log10 = function (x) {
return Math.log(x) / Math.log(10);
}
//Antoine Coefficients
var a_ant=5.402;
var b_ant=1838.7;
var c_ant=-31.7;
//Shift °C to K
CtoK=273.18;
//listens for changes in radio buttons
radioButtons().forEach(function (radioButton) {
radioButton.addEventListener("change", function(e){
solveFor(e.target.value);
});
});
//turn nodelist into array so we can use .forEach
function radioButtons () {
return([].slice.call(document.getElementsByName("unknown")));
};
//adds the class to form based on radio button selection by removing classes of all
//and adding back the class of the one selected --we can use this to hide/show
//input fields based on what we are solving for in CSS
function names () {
return [ "humidity"
, "sampletemperature"
, "bathtemperature"
]
};
function solveFor (name) {
names().forEach(function (other){
document.forms[0].classList.remove(other);
})
document.forms[0].classList.add(name);
solve();
}
function solvingFor () {
return document.forms[0].classList[0];
}
function solve () {
if (solvingFor() == 'humidity') {
computeHumidity()
} else if (solvingFor() == 'sampletemperature') {
computeSampleTemperature()
} else if (solvingFor() == 'bathtemperature') {
computeBathTemperature()
}
}
//changes the user-inputted values into numbers from strings, also
//adds alert to user if it appears they inputted humidity in decimal instead of percent
function inputValueHumidity() {
var humidityInput = parseFloat(document.getElementById("humidity").value);
if (humidityInput < 1 || humidityInput > 100) {
alert("Is your humidity entry correct?\n\nBe sure to enter humidity as a percent, NOT in decimal form.\n\nThis window appeared because you entered a number lower than 1 or higher than 100.");
return(humidityInput);
} else {
return(humidityInput);
}
}
function inputValueSampleTemperatureC() {
return(parseFloat(document.getElementById("sampletemperature").value));
}
function inputValueBathTemperatureC() {
return(parseFloat(document.getElementById("bathtemperature").value));
}
function inputValueSampleTemperatureK() {
return(parseFloat(document.getElementById("sampletemperatureK").value));
}
function inputValueBathTemperatureK() {
return(parseFloat(document.getElementById("bathtemperatureK").value));
}
//uses the inputted values to calculate answers, round to nearest hundredth
//and insert them into the appropriate solution div
State = {
bathTempC: null,
bathTempK: null,
sampleTempC: null,
sampleTempK: null,
humidity: null
}
function cloneState (state) {
return {
bathTempC: state.bathTempC,
bathTempK: state.bathTempK,
sampleTempC: state.sampleTempC,
sampleTempK: state.sampleTempK,
humidity: state.humidity
}
}
function CtoK (c) {
k = c + 273.18;
return k;
}
function KtoC (k) {
c = k - 273.18;
return c;
}
function change (state, field, value, target) {
var newState = cloneState(state);
if (field == "sampleTempC") {
newState.sampleTempC = value;
newState.sampleTempK = CtoK(value);
}
if (field == "sampleTempK") {
newState.sampleTempK = value;
newState.sampleTempC = KtoC(value);
}
if (field == "bathTempC") {
newState.bathTempC = value;
newState.bathTempK = CtoK(value);
}
if (field == "bathTempK") {
newState.bathTempK = value;
newState.bathTempC = KtoC(value);
}
if (field == "humidity") {
newState.humidity = value;
}
if (target == "humidity") solveHumidity(newState);
if (target == "bathTemp") solveBathTemp(newState);
if (target == "sampleTemp") solveSampleTemp(newState);
return newState;
}
var solveHumidity = function() {
}
//equations to solve for the answers
var humidity = function (sampTemp, bathTemp) {
return(100 * Math.pow(10, (-b_ant/(bathTemp+c_ant)+b_ant/(sampTemp+c_ant))));
}
var sampleTemperature = function(relHum, bathTemp) {
relHumE=relHum/100;
return(-c_ant+b_ant/(b_ant/(bathTemp+c_ant)+(log10(relHumE)))-CtoK);
}
var bathTemperature = function(relHum, sampTemp){
relHumE=relHum/100;
return(-c_ant+b_ant/(b_ant/(sampTemp+c_ant)-(log10(relHumE)))-CtoK);
}
function listener () {
// inside of event listener:
State = change(State, "sampleTempC", 123.0);
refreshUI(State);
}
//tests to see if equations work with example data provided by matt
var mustEqual = function(a, b, testName) {
if (a == b) {
console.log("works: " + testName)
} else {
console.log("doesn't work: " + testName)
console.log(" ", a, "doesn't equal", b)
}
}
test = function () {
mustEqual(parseFloat(humidity((25+CtoK), (10.4694+CtoK))).toFixed(1), 40, "humidity celsius test");
mustEqual(parseFloat(sampleTemperature(75, (15+CtoK))).toFixed(4), 19.5491, "sample temperature celsius test");
mustEqual(parseFloat(bathTemperature(82.6, (18+CtoK))).toFixed(1), 15, "sample bath temperature celsius test");
}