Lawds1
10-03-2008, 08:00 PM
Hi, I seemed to have hit a dead end with my script. Please forgive my ignorance. I am new to this and just can't seem to find my mistake. I am pretty sure the problem is with the IF ELSE statements. Any Help with debugging my script would be greatly appreciated.
It is a script to calculate calorific needs for a person, based on Gender, Age, activity level & Weight. It Also calculates the Basic Metabolic Rate.
JavaScript Code:
///// CALCULATE ALL RESULTS /////
function results(){
var bmr=0;
var toMaintain=0;
///// CALCULATE BMR /////
function calcBmr(){
var gender=document.getElementById('gender');
var ageRange=document.getElementById('ageRange');
var weight=parseInt(document.getElementById('weight').value);
///// MALE CALC FOR BMR /////
var m10to18=651+(17.5*weight);
var m19to30=679+(15.3*weight);
var m31to60=879+(11.6*weight);
var m61plus=487+(13.5*weight);
///// FEMALE CALC FOR BMR /////
var f10to18=746+(12.2*weight);
var f19to30=479+(14.7*weight);
var f31to60=829+(8.7*weight);
var f61plus=596+(10.5*weight);
if (gender.options[gender.selectedIndex].value=='male'){
if (ageRange.options[ageRange.selectedIndex].value=='tentoeighteen')
bmr=m10to18;
else if(ageRange.options[ageRange.selectedIndex].value=='nineteentothirty')
bmr=m19to30;
else if(ageRange.options[ageRange.selectedIndex].value=='thirtyonetosixty')
bmr=m31to60;
else (ageRange.options[ageRange.selectedIndex].value=='sixtyoneplus')
bmr=m61plus;
}
else {
if (ageRange.options[ageRange.selectedIndex].value=='tentoeighteen')
bmr=f10to18;
else if(ageRange.options[ageRange.selectedIndex].value=='nineteentothirty')
bmr=f19to30;
else if(ageRange.options[ageRange.selectedIndex].value=='thirtyonetosixty')
bmr=f31to60;
else(ageRange.options[ageRange.selectedIndex].value=='sixtyoneplus')
bmr=f61plus;
}
}
///// CALC CALORIES TO MAINTAIN WEIGHT /////
function toMaintaincalc(){
INACTIVE1=1.2;
FAIRACT1=1.3;
MODACTIVE1=1.4;
ACTIVE1=1.5;
VACT1=1.7;
var activeLevel=document.getElementById('activeLevel');
if (activeLevel.options[activeLevel.selectedIndex].value=='inactive')
toMaintain=bmr*INACTIVE1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='fairact')
toMaintain=bmr*FAIRACT1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='modact')
toMaintain=bmr*MODACTIVE1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='active')
toMaintain=bmr*ACTIVE1;
else(activeLevel.options[activeLevel.selectedIndex].value=='vact')
toMaintain=bmr*VACT1;
}
///// RETURN ANSWER FOR BMR /////
document.getElementById('bmr').value=bmr+" Calories";
///// RETURN ANSWER TO MAINTAIN WEIGHT /////
document.getElementById('maintain').value=toMaintain+" Calories";
///// CALC CALORIES TO LOSE WEIGHT ////
FATLOSS1=0.85;
var fatLoss=toMaintain*FATLOSS1;
///// RETURN ANSWER TO LOSE WEIGHT /////
document.getElementById('lose').value=fatLoss+" Calories";
///// CALC CALORIES TO GAIN WEIGHT /////
MUSCLEGAIN1=1.2;
var weightGain=toMaintain*MUSCLEGAIN1;
///// RETURN ANSWER TO GAIN WEIGHT /////
document.getElementById('gain').value=weightGain+" Calories";
}
HTML Code:
<body>
<div id="mainhead"><h1 id="mainheading"> DAILY CALORIFIC NEEDS CALCULATOR</h1></div>
<div id="calform">
<table id="table1" border="1">
<tr>
<td>
<form id="form1" name="form1">
<!-- GENDER -->
<h3>Please Select Your Gender:</h3>
<select id="Gender" name="Gender">
<option selected="selected"> <- Select Gender -></option>
<option id="male" name="male">Male</option>
<option id="Female" name="Female">Female</option>
</select>
<!-- AGE -->
<h3>Please Select Your Age Range</h3>
<select id="ageRange" name="ageRange">
<option selected="selected"><- Select Age Range -></option>
<option id="tentoeighteen" name="tentoeighteen">10 - 18</option>
<option id="nineteentothirty" name="nineteentothirty">19 - 30</option>
<option id="thirtyonetosixty" name="thirtyonetosixty">31 - 60</option>
<option id="sixtyoneplus" name="sixtyoneplus">61+</option>
</select>
<!-- ACTIVITY LEVEL -->
<h3>Please Select Your Activity Level</h3>
<select id="activelevel" name="activelevel">
<option selected="selected"><- Select Activity Level -></option>
<option id="inactive" name="inactive">Mostly inactive or sedentary (Mainly Sitting)</option>
<option id="fairact" name="fairact">Fairly Active (Include Walking & Exercice 1-2x week)</option>
<option id="modact" name="modact">Moderatly Active (Exercise 2-3x week)</option>
<option id="active" name="active">Active (Exercise hard more than 3x week)</option>
<option id="vact" name="vact">Very Active (Exercise hard daily)</option>
</select>
<!-- WEIGHT -->
<h3>Please Input your Weight in KG</h3>
<input type="text" id="weight" name="weight" value="" />
<br />
<!-- BUTTONS -->
<input type="button" value="<< Calculate >>" id="calculate" name="calculate" onclick="results();" />
<input type="reset" value="<< Reset >>" id="reset" name="reset" />
<br />
<br />
<br />
<!-- BMR ANSWER -->
Your BMR is:
<input type="text" id="bmr" name="bmr" value="" readonly="readonly" />
<!-- RESULTS -->
<h3 id="head3">These are Your Daily Calorific Needs</h3>
<!-- MAINTAIN WEIGHT ANSWER -->
To Maintain Weight:
<input type="text" id="maintain" name="maintain" readonly="readonly" value="" />
<br />
<br />
<!-- LOSE WEIGHT ANSWER -->
To Lose Fat:
<input type="text" id="lose" name="lose" readonly="readonly" value="" />
<br />
<br />
<!-- GAIN WEIGHT ANSWER -->
To Gain Weight:
<input type="text" id="gain" name="gain" readonly="readonly" value="" />
<br />
</form>
</td>
</tr>
</table>
</div>
</body>
It is a script to calculate calorific needs for a person, based on Gender, Age, activity level & Weight. It Also calculates the Basic Metabolic Rate.
JavaScript Code:
///// CALCULATE ALL RESULTS /////
function results(){
var bmr=0;
var toMaintain=0;
///// CALCULATE BMR /////
function calcBmr(){
var gender=document.getElementById('gender');
var ageRange=document.getElementById('ageRange');
var weight=parseInt(document.getElementById('weight').value);
///// MALE CALC FOR BMR /////
var m10to18=651+(17.5*weight);
var m19to30=679+(15.3*weight);
var m31to60=879+(11.6*weight);
var m61plus=487+(13.5*weight);
///// FEMALE CALC FOR BMR /////
var f10to18=746+(12.2*weight);
var f19to30=479+(14.7*weight);
var f31to60=829+(8.7*weight);
var f61plus=596+(10.5*weight);
if (gender.options[gender.selectedIndex].value=='male'){
if (ageRange.options[ageRange.selectedIndex].value=='tentoeighteen')
bmr=m10to18;
else if(ageRange.options[ageRange.selectedIndex].value=='nineteentothirty')
bmr=m19to30;
else if(ageRange.options[ageRange.selectedIndex].value=='thirtyonetosixty')
bmr=m31to60;
else (ageRange.options[ageRange.selectedIndex].value=='sixtyoneplus')
bmr=m61plus;
}
else {
if (ageRange.options[ageRange.selectedIndex].value=='tentoeighteen')
bmr=f10to18;
else if(ageRange.options[ageRange.selectedIndex].value=='nineteentothirty')
bmr=f19to30;
else if(ageRange.options[ageRange.selectedIndex].value=='thirtyonetosixty')
bmr=f31to60;
else(ageRange.options[ageRange.selectedIndex].value=='sixtyoneplus')
bmr=f61plus;
}
}
///// CALC CALORIES TO MAINTAIN WEIGHT /////
function toMaintaincalc(){
INACTIVE1=1.2;
FAIRACT1=1.3;
MODACTIVE1=1.4;
ACTIVE1=1.5;
VACT1=1.7;
var activeLevel=document.getElementById('activeLevel');
if (activeLevel.options[activeLevel.selectedIndex].value=='inactive')
toMaintain=bmr*INACTIVE1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='fairact')
toMaintain=bmr*FAIRACT1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='modact')
toMaintain=bmr*MODACTIVE1;
else if(activeLevel.options[activeLevel.selectedIndex].value=='active')
toMaintain=bmr*ACTIVE1;
else(activeLevel.options[activeLevel.selectedIndex].value=='vact')
toMaintain=bmr*VACT1;
}
///// RETURN ANSWER FOR BMR /////
document.getElementById('bmr').value=bmr+" Calories";
///// RETURN ANSWER TO MAINTAIN WEIGHT /////
document.getElementById('maintain').value=toMaintain+" Calories";
///// CALC CALORIES TO LOSE WEIGHT ////
FATLOSS1=0.85;
var fatLoss=toMaintain*FATLOSS1;
///// RETURN ANSWER TO LOSE WEIGHT /////
document.getElementById('lose').value=fatLoss+" Calories";
///// CALC CALORIES TO GAIN WEIGHT /////
MUSCLEGAIN1=1.2;
var weightGain=toMaintain*MUSCLEGAIN1;
///// RETURN ANSWER TO GAIN WEIGHT /////
document.getElementById('gain').value=weightGain+" Calories";
}
HTML Code:
<body>
<div id="mainhead"><h1 id="mainheading"> DAILY CALORIFIC NEEDS CALCULATOR</h1></div>
<div id="calform">
<table id="table1" border="1">
<tr>
<td>
<form id="form1" name="form1">
<!-- GENDER -->
<h3>Please Select Your Gender:</h3>
<select id="Gender" name="Gender">
<option selected="selected"> <- Select Gender -></option>
<option id="male" name="male">Male</option>
<option id="Female" name="Female">Female</option>
</select>
<!-- AGE -->
<h3>Please Select Your Age Range</h3>
<select id="ageRange" name="ageRange">
<option selected="selected"><- Select Age Range -></option>
<option id="tentoeighteen" name="tentoeighteen">10 - 18</option>
<option id="nineteentothirty" name="nineteentothirty">19 - 30</option>
<option id="thirtyonetosixty" name="thirtyonetosixty">31 - 60</option>
<option id="sixtyoneplus" name="sixtyoneplus">61+</option>
</select>
<!-- ACTIVITY LEVEL -->
<h3>Please Select Your Activity Level</h3>
<select id="activelevel" name="activelevel">
<option selected="selected"><- Select Activity Level -></option>
<option id="inactive" name="inactive">Mostly inactive or sedentary (Mainly Sitting)</option>
<option id="fairact" name="fairact">Fairly Active (Include Walking & Exercice 1-2x week)</option>
<option id="modact" name="modact">Moderatly Active (Exercise 2-3x week)</option>
<option id="active" name="active">Active (Exercise hard more than 3x week)</option>
<option id="vact" name="vact">Very Active (Exercise hard daily)</option>
</select>
<!-- WEIGHT -->
<h3>Please Input your Weight in KG</h3>
<input type="text" id="weight" name="weight" value="" />
<br />
<!-- BUTTONS -->
<input type="button" value="<< Calculate >>" id="calculate" name="calculate" onclick="results();" />
<input type="reset" value="<< Reset >>" id="reset" name="reset" />
<br />
<br />
<br />
<!-- BMR ANSWER -->
Your BMR is:
<input type="text" id="bmr" name="bmr" value="" readonly="readonly" />
<!-- RESULTS -->
<h3 id="head3">These are Your Daily Calorific Needs</h3>
<!-- MAINTAIN WEIGHT ANSWER -->
To Maintain Weight:
<input type="text" id="maintain" name="maintain" readonly="readonly" value="" />
<br />
<br />
<!-- LOSE WEIGHT ANSWER -->
To Lose Fat:
<input type="text" id="lose" name="lose" readonly="readonly" value="" />
<br />
<br />
<!-- GAIN WEIGHT ANSWER -->
To Gain Weight:
<input type="text" id="gain" name="gain" readonly="readonly" value="" />
<br />
</form>
</td>
</tr>
</table>
</div>
</body>