...

View Full Version : Need help debugging IF ELSE statement!



Lawds1
10-07-2008, 08:42 PM
Hi I need a little help with debugging my code as I can't for the life of me find the problem. My code works fine in Internet Explorer but not in Firefox.
I would like to say though, that I know there are better ways of codeing, other that IF ELSE, but this is how I want to do it.

Please help

Javascript:



///// CALCULATE ALL RESULTS /////
function results(){
var bmr=0;
var msg="";
var toMaintain=0;

///// CALCULATE BMR /////
var genderSel=document.getElementById('Gender');
if (genderSel.options[genderSel.selectedIndex].value=='')
msg=alert("Please Select Your Gender");

var ageRangeSel=document.getElementById('ageRange');
if (ageRangeSel.options[ageRangeSel.selectedIndex].value=='')
msg=alert("Please Select Your Age Range");

var weight=parseInt(document.getElementById('weight').value);
if (isNaN(weight))
weight=0;
if (weight<1)
msg=alert("Please Enter Your Weight in KG's");

///// MALE CALC FOR BMR /////
var m10to18=(17.5*weight)+651;
var m19to30=(15.3*weight)+679;
var m31to60=(11.6*weight)+879;
var m61plus=(13.5*weight)+487;

///// 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 (genderSel.options[genderSel.selectedIndex].value=='male'){
if (ageRangeSel.options[ageRangeSel.selectedIndex].value=='tentoeighteen')
bmr=m10to18;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='nineteentothirty')
bmr=m19to30;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='thirtyonetosixty')
bmr=m31to60;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='sixtyoneplus')
bmr=m61plus;
}

else if (genderSel.options[genderSel.selectedIndex].value=='female'){
if (ageRangeSel.options[ageRangeSel.selectedIndex].value=='tentoeighteen')
bmr=f10to18;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='nineteentothirty')
bmr=f19to30;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='thirtyonetosixty')
bmr=f31to60;
else if(ageRangeSel.options[ageRangeSel.selectedIndex].value=='sixtyoneplus')
bmr=f61plus;
}


///// CALC CALORIES TO MAINTAIN WEIGHT /////
INACTIVE1=1.2;
FAIRACT1=1.3;
MODACTIVE1=1.4;
ACTIVE1=1.5;
VACT1=1.7;

var activelevelSel=document.getElementById('activeLevel');
if (activelevelSel.options[activelevelSel.selectedIndex].value=='')
msg=alert("Please Select Your Activity Level");

if (activelevelSel.options[activelevelSel.selectedIndex].value=='inactive')
toMaintain=bmr*INACTIVE1;
else if(activelevelSel.options[activelevelSel.selectedIndex].value=='fairact')
toMaintain=bmr*FAIRACT1;
else if(activelevelSel.options[activelevelSel.selectedIndex].value=='modact')
toMaintain=bmr*MODACTIVE1;
else if(activelevelSel.options[activelevelSel.selectedIndex].value=='active')
toMaintain=bmr*ACTIVE1;
else if(activelevelSel.options[activelevelSel.selectedIndex].value=='vact')
toMaintain=bmr*VACT1;


///// CALC CALORIES TO LOSE WEIGHT ////
FATLOSS1=0.85;
var fatLoss=toMaintain*FATLOSS1;

///// CALC CALORIES TO GAIN WEIGHT /////
MUSCLEGAIN1=1.2;
var weightGain=toMaintain*MUSCLEGAIN1;

///// RETURN ANSWER FOR BMR /////
document.getElementById('bmr').value=bmr.toFixed(0)+" Calories";

///// RETURN ANSWER TO MAINTAIN WEIGHT /////
document.getElementById('maintain').value=toMaintain.toFixed(0)+" Calories";

///// RETURN ANSWER TO LOSE WEIGHT /////
document.getElementById('lose').value=fatLoss.toFixed(0)+" Calories";

///// RETURN ANSWER TO GAIN WEIGHT /////
document.getElementById('gain').value=weightGain.toFixed(0)+" Calories";

}



HTML:



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Calorific Calculations</title>

<script type="text/javascript" src="Calculations2.js"></script>
<link rel="stylesheet" type="text/css" href="Calculations.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
<div id="mainhead"><h3 id="mainheading"> DAILY CALORIFIC NEEDS CALCULATOR</h3></div>

<div id="pic"><img src="runner.jpg" id="picture" alt="runner.jpg" /></div>

<div id="calform">
<table id="table1" border="">
<tr>
<td>
<form id="form1" name="form1">

<!-- GENDER -->
<h3>Please Select Your Gender:</h3>

<select id="Gender" name="Gender">
<option selected="selected" value=""> <- Select Gender -> </option>
<option id="male" name="male" value="male">Male</option>
<option id="Female" name="Female" value="female">Female</option>
</select>

<!-- AGE -->
<h3>Please Select Your Age Range</h3>

<select id="ageRange" name="ageRange">
<option selected="selected" value=""> <- Select Age Range -> </option>
<option id="tentoeighteen" name="tentoeighteen" value="tentoeighteen">10 - 18</option>
<option id="nineteentothirty" name="nineteentothirty" value="nineteentothirty">19 - 30</option>
<option id="thirtyonetosixty" name="thirtyonetosixty" value="thirtyonetosixty">31 - 60</option>
<option id="sixtyoneplus" name="sixtyoneplus" value="sixtyoneplus">61+</option>
</select>

<!-- ACTIVITY LEVEL -->
<h3>Please Select Your Activity Level</h3>

<select id="activeLevel" name="activeLevel">
<option selected="selected" value=""> <---------------------- Select Activity Level ----------------------> </option>
<option id="inactive" name="inactive" value="inactive">Mostly inactive or sedentary (Mainly Sitting)</option>
<option id="fairact" name="fairact" value="fairact">Fairly Active (Include Walking & Exercice 1-2x week)</option>
<option id="modact" name="modact" value="modact">Moderatly Active (Exercise 2-3x week)</option>
<option id="active" name="active" value="active">Active (Exercise hard more than 3x week)</option>
<option id="vact" name="vact" value="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 /><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 />

<!-- RESULTS -->
<h3 id="head3">These are Your Daily Calorific Needs</h3>

<!-- BMR ANSWER -->
Your BMR is:
<input type="text" id="bmr" name="bmr" value="0 Calories" readonly="readonly" />
<br />
<br />

<!-- LOSE WEIGHT ANSWER -->
To Lose Fat:
<input type="text" id="lose" name="lose" readonly="readonly" value="0 Calories" />
<br />
<br />

<!-- MAINTAIN WEIGHT ANSWER -->
To Maintain Weight:
<input type="text" id="maintain" name="maintain" readonly="readonly" value="0 Calories" />
<br />
<br />

<!-- GAIN WEIGHT ANSWER -->
To Gain Weight:
<input type="text" id="gain" name="gain" readonly="readonly" value="0 Calories" />
<br />
<br />

</form>
</td>
</tr>
</table>
</div>

</body>
</html>

ohgod
10-07-2008, 09:30 PM
what is the error you're getting in FF?


and as always, firebug is handy for troubleshooting in FF, you might consider getting it if you haven't already

Lawds1
10-07-2008, 11:41 PM
Thanks I have downloaded firebug (really cool).
This is the message I get, but I am not sure what it means.

activelevelSel is null
results()Calculations2.js (line 65)
onclick()calc2.html (line 1)
[Break on this error] if (activelevelSel.options[activelevelSel.selectedIndex].value=='')



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum