
Originally Posted by
BrianBam
A couple starting questions that I have are
1) How do I leave the variable blank? var Years; and Var RestingHeartRate;
You don't need to declare those variables where you have them in your existing code. They are all used only within the function, so their declaration as the function arguments is enough.
Code:
var Years;
var RestingHeartRate;
var BaseHeartRate=220;
function CalculateFatHeartRates(Years, RestingHeartRate, BaseHeartRate)
{
...
}
So now we have the question of how do we get those variables to the function?

Originally Posted by
BrianBam
2) Do I have to run a loop to get the data that is entered into the fields??
Yes, that question.
To do that we remove the inline event attribute (it doesn't belong there) so that we can use a more flexible solution via scripting instead.
Code:
<form action="" id="HeartRateForm" onchange="CalculateFatHeartRates()">
Code javascript:
document.getElementById('HeartRateForm').onchange = function () {
var form = this,
years = parseFloat(form.elements.Years.value) || 0,
restingHeartRate = parseFloat(form.elements.RestingHeartRate.value) || 0,
baseHeartRate = 220;
CalculateFatHeartRates(years, restingHeartRate, baseHeartRate);
};
Using "|| 0" allows us to default to a value of 0, if the form doesn't contain a numeric value.
You will also pick up over time that function and variable names should start as lowercase. The first letter tends to only capitalised in rare situations, such as when the function is a special Constructor, or when a variable is all uppercase to indicate that it's supposed to be a constant.
Bookmarks