Creatinine Clearance Calculator Javascript Logic
// set up our units of conversions, could be shared between multiple calculators | |
var units = { | |
"age": {"yr": 1, "mth": 12.0}, | |
"weight": {"kg": 1, "lb": 2.20462}, | |
"creatinine": {"gm/L": 0.01, "gm/dL": 0.001, "mg%": 1, "mg/dL": 1, "mg/mL": 0.01}, | |
"output": {"mL/min": 1, "L/hr": 0.06} | |
}; | |
// get the inputs specific to this calculator (irl, this would come from the form fields) | |
var inputs = { | |
"sex": {"value": "male"}, | |
"age": {"value": 30, "unit": "yr"}, | |
"weight": {"value": 150, "unit": "lb"}, | |
"creatinine": {"value": 0.008, "unit": "gm/L"} | |
}; | |
// standardize all the inputs in the units of our equation | |
var standardized = {}; | |
for (var key in inputs) { | |
if ("unit" in inputs[key] || !inputs[key]["unit"]) { | |
standardized[key] = inputs[key]["value"] / units[key][inputs[key]["unit"]]; | |
} else{ | |
standardized[key] = inputs[key]["value"]; // ignore those unit less inputs | |
} | |
} | |
standardized["output"] = (standardized["sex"] == "male" ? 1.0 : 0.85) * (140 - standardized["age"]) * standardized["weight"] / (72 * standardized["creatinine"]); | |
output = standardized["output"] * units["output"]["mL/min"]; // desired output unit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment