I'm making a page which asks the user a question, grabs the result, grabs the variables assigned to that result and inputs them into a table. What I've done so far works perfectly, but it's not efficient and I know there's a better way to do it using arrays and possibly loops, but every iteration I've tried still results in equal to or more work. Here's what I've created (which works fine):
function agecalc() {
var weight;
var HRlow;
var HRhi;
var RRlow;
var RRhi;
var SBPlow;
var SBPhi;
var DBPlow;
var DBPhi;
var age = parseFloat(document.getElementById("agechoice").value);
if (isNaN(age)) {
var weight = 0
} else if (age == 0) {
var weight = 3.5
} else if (age == 0.5) {
var weight = 8
} else if (age == 8) {
var weight = 25
} else if (age == 9) {
var weight = 28
} else if (age == 10) {
var weight = 32
} else if (age == 11) {
var weight = 36
} else {
var weight = (age + 4) * 2
}
// Observations determinant
if (weight >= 0 && weight <= 5) {
var HRlow = 100,
HRhi = 160,
RRlow = 30,
RRhi = 50,
SBPlow = 75,
SBPhi = 100,
DBPlow = 50,
DBPhi = 70;
} else if (weight > 5 && weight <= 9) {
var HRlow = 80,
HRhi = 140,
RRlow = 20,
RRhi = 30,
SBPlow = 75,
SBPhi = 100,
DBPlow = 50,
DBPhi = 70;
} else if (weight > 9 && weight <= 11) {
var HRlow = 80,
HRhi = 130,
RRlow = 20,
RRhi = 30,
SBPlow = 80,
SBPhi = 110,
DBPlow = 50,
DBPhi = 80;
} else if (weight > 11 && weight <= 13) {
var HRlow = 80,
HRhi = 130,
RRlow = 20,
RRhi = 30,
SBPlow = 80,
SBPhi = 110,
DBPlow = 50,
DBPhi = 80;
} else if (weight > 13 && weight <= 15) {
var HRlow = 80,
HRhi = 130,
RRlow = 20,
RRhi = 30,
SBPlow = 80,
SBPhi = 110,
DBPlow = 50,
DBPhi = 80;
} else if (weight > 15 && weight <= 17) {
var HRlow = 80,
HRhi = 120,
RRlow = 20,
RRhi = 30,
SBPlow = 80,
SBPhi = 110,
DBPlow = 50,
DBPhi = 80;
} else if (weight > 17 && weight <= 19) {
var HRlow = 80,
HRhi = 120,
RRlow = 20,
RRhi = 30,
SBPlow = 80,
SBPhi = 110,
DBPlow = 50,
DBPhi = 80;
} else if (weight > 19 && weight <= 21) {
var HRlow = 70,
HRhi = 110,
RRlow = 15,
RRhi = 30,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 21 && weight <= 23.5) {
var HRlow = 70,
HRhi = 110,
RRlow = 15,
RRhi = 30,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 23.5 && weight <= 26.5) {
var HRlow = 70,
HRhi = 110,
RRlow = 15,
RRhi = 30,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 26.5 && weight <= 30) {
var HRlow = 70,
HRhi = 110,
RRlow = 15,
RRhi = 30,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 30 && weight <= 34) {
var HRlow = 70,
HRhi = 110,
RRlow = 15,
RRhi = 30,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 34 && weight <= 38) {
var HRlow = 60,
HRhi = 105,
RRlow = 15,
RRhi = 20,
SBPlow = 85,
SBPhi = 120,
DBPlow = 55,
DBPhi = 80;
} else if (weight > 38 && weight <= 49) {
var HRlow = 60,
HRhi = 100,
RRlow = 12,
RRhi = 20,
SBPlow = 80,
SBPhi = 120,
DBPlow = 60,
DBPhi = 80;
} else if (weight > 49 && weight <= 69) {
var HRlow = 60,
HRhi = 100,
RRlow = 12,
RRhi = 20,
SBPlow = 80,
SBPhi = 120,
DBPlow = 60,
DBPhi = 80;
} else if (weight > 69 && weight <= 99) {
var HRlow = 60,
HRhi = 100,
RRlow = 12,
RRhi = 20,
SBPlow = 80,
SBPhi = 120,
DBPlow = 60,
DBPhi = 80;
} else if (weight >= 100) {
var HRlow = 60,
HRhi = 100,
RRlow = 12,
RRhi = 20,
SBPlow = 80,
SBPhi = 120,
DBPlow = 60,
DBPhi = 80;
}
// Place observations into the table
document.getElementById("HRlow").innerHTML = HRlow;
document.getElementById("HRhi").innerHTML = HRhi;
document.getElementById("RRlow").innerHTML = RRlow;
document.getElementById("RRhi").innerHTML = RRhi;
document.getElementById("SBPlow").innerHTML = SBPlow;
document.getElementById("SBPhi").innerHTML = SBPhi;
document.getElementById("DBPlow").innerHTML = DBPlow;
document.getElementById("DBPhi").innerHTML = DBPhi;
}
<table>
<tr>
<td>Age</td>
<td><select id="agechoice" onchange="agecalc()">
<option selected="selected">
- Select Age -
</option>
<option value="defaultvalue">
</option>
<option value="0">
Newborn
</option>
<option value=".5">
6 months
</option>
<option value="1">
1 year
</option>
<option value="2">
2 years
</option>
<option value="3">
3 years
</option>
<option value="4">
4 years
</option>
<option value="5">
5 years
</option>
<option value="6">
6 years
</option>
<option value="7">
7 years
</option>
<option value="8">
8 years
</option>
<option value="9">
9 years
</option>
<option value="10">
10 years
</option>
<option value="11">
11 years
</option>
</select></td>
</tr>
</table>
<table id="observations">
<tbody>
<tr>
<td><strong>Lower limit</strong></td>
<td><strong>Upper limit</strong></td>
</tr>
<tr>
<td colspan="2">Heart Rate</td>
</tr>
<tr>
<td id="HRlow"> </td>
<td id="HRhi"> </td>
</tr>
<tr>
<td colspan="2">Respiratory Rate</td>
</tr>
<tr>
<td id="RRlow"> </td>
<td id="RRhi"> </td>
</tr>
<tr>
<td colspan="2">Systolic Blood Pressure</td>
</tr>
<tr>
<td id="SBPlow"> </td>
<td id="SBPhi"> </td>
</tr>
<tr>
<td colspan="2">Diastolic Blood Pressure</td>
</tr>
<tr>
<td id="DBPlow"> </td>
<td id="DBPhi"> </td>
</tr>
</tbody>
</table>
I've thought about using arrays like this:
const HRlow = [100,80,80,80,]
const HRhi = [160,140,130,130,120]
const RRlow = [30,20,20,20]
But then wouldn't I have to, under each grouping (for example: (weight >= 0 && weight <= 5)), have to put:
document.getElementById("HRlow").innerHTML = HRlow[1];
document.getElementById("HRhi").innerHTML = HRhi[1];
etc., thus resulting in more code.
I've thought about setting the array by age group, e.g.
const obs1 = [100,160,30,50,75,100,50,70];
But then I would have to again, under each grouping, I would still be entering each iteration of document.getElementById and selecting the object. I know there's a way to do it more efficiently (maybe with loops?) I just can't put my finger on it.