Forum Moderators: open
( <input id="v1" type="text"> -
<input id="v2" type="text"> ) x
<input id="v3" type="text">
<input type="button" id="calculate" value="=">
<input id="result">
body {
padding: 1em;
}
input[type="text"]:focus {
background-color: #eee;
}
(function () {
var v1 = document.getElementById('v1'),
v2 = document.getElementById('v2'),
v3 = document.getElementById('v3'),
calculate = document.getElementById('calculate'),
result = document.getElementById('result');
function validate(el) {
if (el.value.length == 0 ||
/\D/.test(el.value)) {
el.focus();
result.value = "Error in input";
return false;
}
return true;
}
calculate.onclick = function () {
if (validate(v1) && validate(v2) && validate(v3)) {
result.value = 0 + ((v1.value - v2.value) * v3.value);
}
};
})();
the way Fotiman coded it - it's 144.
as written by the OP, PEMDAS kicks in and it's 300.
That's the mind of math you end up with when you're burning the mid nite oil.
the problem is that line 239 contains the file name of the config file which is not a valid Apache directive
Is thee a simpler way to code this so I can edit in Frontpage?
( <input id="systolic" type="text"> -
<input id="diastolic" type="text"> ) x
<input id="hereditaryAndLifestyle" type="text">
<input type="button" id="calculate" value="=">
<input id="result">
(function () {
var systolic = document.getElementById('systolic'),
diastolic = document.getElementById('diastolic'),
hereditaryAndLifestyle = document.getElementById('hereditaryAndLifestyle'),
calculate = document.getElementById('calculate'),
result = document.getElementById('result');
// Verify that the input is numeric and not empty
function validate(el) {
if (el.value.length == 0 ||
/\D/.test(el.value)) {
el.focus();
result.value = "Error in input";
return false;
}
return true;
}
// Attach handler to the calculate button
calculate.onclick = function () {
if (validate(systolic) &&
validate(diastolic) &&
validate(hereditaryAndLifestyle)) {
// Calculate the risk as (systolic - diastolic) x hereditary & lifestyle risk
result.value = 0 + ((systolic.value - diastolic.value) * hereditaryAndLifestyle.value);
}
};
})();