...

View Full Version : Need help with IF ELSE statments



Lawds1
10-03-2008, 09: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>

mattyod
10-04-2008, 12:16 PM
With that many "if" statements going on you might want to consider using switch statements (http://www.w3schools.com/js/js_switch.asp) instead.

Lawds1
10-04-2008, 02:38 PM
I would and probably will at a later stage, but I am trying to learn and master javascript and I don't think I will if I can't make my script work using the IF ELSE statments. I need to find why it's not working and what I am doing wrong. So any help to my learning will be much appreciated.

Cranford
10-04-2008, 04:00 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var maleBMRfactor = [0, 17.5, 15.3, 11.6, 13.5];
var femaleBMRfactor = [0, 12.2, 14.7, 8.7, 10.5];

function calculate(nForm){

var nGender = nForm['gender'].value;
var nAgeRange = nForm['ageRange'].value.split("|");
var nActivityLevel = nForm['activityLevel'].value;
var nWeight = nForm['weight'].value;
if (nGender == "" || nAgeRange == "" || nActivityLevel == "" || nWeight == "")
{
alert('Incomplete Information');
return false;
}
if (nGender == "male")
{
nForm['bmr'].value = Number(nAgeRange[0]) + Number((nWeight * maleBMRfactor[nForm['ageRange'].selectedIndex]));
}
else {
nForm['bmr'].value = Number(nAgeRange[1]) + Number((nWeight * femaleBMRfactor[nForm['ageRange'].selectedIndex]));
}
nForm['maintain'].value = Math.round(nForm['bmr'].value * nForm['activityLevel'].value);
nForm['lose'].value = Math.round(nForm['maintain'].value * .85);
nForm['gain'].value = Math.round(nForm['maintain'].value * 1.2);
return false;
}

</script>
<style type="text/css">

body {background-color: #eae3c6; margin-top: 60px;}
form {width: 620px; margin: auto; font-family: times; font-size: 12pt;}
fieldset {width: 620px; padding-left: 10px; padding-top: 25px; padding-bottom: 25px; background-color: #f0fff0; border: 1px solid #87ceeb;}
legend {font-family: times; font-size: 14pt; color: #00008b; background-color: #87ceeb; padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
#mainhead {width: 500px; margin: auto;}
.submitBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}
.resetBtn {font-family: tahoma; font-size: 10pt; display: block; margin-left: auto; margin-right: auto; margin-top: 5px; margin-bottom: 5px;}

</style>
</head>
<body>
<div id="mainhead">
<h2>
DAILY CALORIFIC NEEDS CALCULATOR
</h2>
</div>
<form action="" onsubmit="return calculate(this)">
<fieldset>
<h3>
Please Select Your Gender:
</h3><select name="gender">
<option value="">
Select Gender
</option>
<option value="male">
Male
</option>
<option value="female">
Female
</option>
</select>
<h3>
Please Select Your Age Range
</h3><select name="ageRange">
<option value="">
Select Age Range
</option>
<option value="651|746">
10 - 18
</option value="679|479">
<option>
19 - 30
</option>
<option value="879|829">
31 - 60
</option>
<option value="487|596">
61+
</option>
</select>
<h3>
Please Select Your Activity Level
</h3>
<select name="activityLevel">
<option value="">
Select Activity Level
</option>
<option value="1.2">
Mostly inactive or sedentary (Mainly Sitting)
</option>
<option value="1.3">
Fairly Active (Include Walking &amp; Exercice 1-2x week)
</option>
<option value="1.4>
Moderatly Active (Exercise 2-3x week)
</option>
<option value="1.5">
Active (Exercise hard more than 3x week)
</option>
<option value="1.7">
Very Active (Exercise hard daily)
</option>
</select>
<h3>
Please Input your Weight in KG
</h3><input type="text" name="weight">
<br>
<input type="submit" value="Calculate" class="submitBtn"> <input type="reset" value="Reset" class="resetBtn">
<br>
<br>
Your BMR is: <input type="text" name="bmr" readonly>
<h3>
These are Your Daily Calorific Needs
</h3>To Maintain Weight: <input type="text" name="maintain" readonly><br>
<br>
To Lose Fat: <input type="text" name="lose" readonly><br>
<br>
To Gain Weight: <input type="text" name="gain" readonly><br>
</fieldset>
</form>
</body>
</html>

Lawds1
10-04-2008, 06:51 PM
Thanks so much for that, it works great.

I would like to know though, would the code, that I had done, have worked with a few minor adjustments or would that have been impossible?

I know there are simpler ways of doing it, but as described earlier, I am a complete novice at javascript and I don't think I should move on to those simpler ways until I have fully grasped the IF ELSE statments and nesting them. I thought they were correct, but obviously I am missing something.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum