...

View Full Version : Please Help to debugg my script (Switch/case)



Lawds1
10-14-2008, 01:19 PM
Hi I would really appreciat some help with using the SWITCH/CASE script. I am still learning javascript so any help would be appreciated.
I managed to get my script to work using IF/ELSE, but now I am trying to use the SWITCH/CASE script, but it is not working. I can't seem to find my mistake. Please help.

javascript:



<script type="text/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";

}

</script>



HTML:



<body>
<h2 id="mainheading"> <font color="#000000">DAILY CALORIFIC NEEDS CALCULATOR</font></h2>
<div id="calform">
<table id="table1" border="">
<tr>
<td> <form id="form1" name="form1" title="Calculation Form for Daily Calorific Needs">


<!-- GENDER -->
<p><font color="#666666"><strong>Select Your Gender:</strong></font>
<select name="Gender" id="Gender">
<option value="" selected="selected">Select Gender</option>
<option value="male" id="male" name="male">Male</option>
<option id="Female" name="Female" value="female">Female</option>
</select>
</p>
<br />

<!-- AGE -->
<p><font color="#666666"><strong>Select Your Age Range:</strong></font>
<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>
</p>
<br />

<!-- ACTIVITY LEVEL -->
<p> <font color="#666666"><strong>Select Your Activity Level:</strong></font>
<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>
</p>
<br />

<!-- WEIGHT -->
<p> <font color="#666666"><strong>Input your Weight in KG:</strong></font>
<input type="text" id="weight" name="weight" value="" />
</p>
<br />

<!-- BUTTONS -->
<input type="button" value="<< Calculate >>" title="Calculate" id="calculate" name="calculate" onclick="results();" />
<input type="reset" value="<< Reset >>" title="Reset" id="reset" name="reset" />
<br />
<br />
<br />

<!-- RESULTS -->
<h3 id="head3"><font color="#2a2a2a">These are Your Daily Calorific
Needs</font></h3>

<!-- BMR ANSWER -->
<font color="#666666"><strong>Your BMR is:</strong>
<input type="text" title="BMR" id="bmr" name="bmr" value="0 Calories" readonly="readonly" />
<br />
<br />

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

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

<!-- GAIN WEIGHT ANSWER -->
<strong>To Gain Weight:</strong></font>
<input type="text" id="gain" name="gain" readonly="readonly" value="0 Calories" />
<br />
<br />
</form></td>
</tr>
</table>
</div>
</body>

vwphillips
10-14-2008, 02:49 PM
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);
var sex=genderSel.options[genderSel.selectedIndex].value;
var age=ageRangeSel.options[ageRangeSel.selectedIndex].value;
switch (sex){
case 'male':
switch (age) {
case 'tentoeighteen':
bmr=m19to30;
break;
case 'nineteentothirty':
bmr=m19to30;
break;
case 'thirtyonetosixty':
bmr=m31to60;
break;
case 'sixtyoneplus':
bmr=m61plus;
break;
default:
break;
}
break;
case 'female':
switch (age) {
case 'tentoeighteen':
bmr=f19to30;
break;
case 'nineteentothirty':
bmr=f19to30;
break;
case 'thirtyonetosixty':
bmr=f31to60;
break;
case 'sixtyoneplus':
bmr=f61plus;
break;
default:
break;
}
break;
default:
break;
}

///// 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";

}


//-->
</script>

Kor
10-14-2008, 02:54 PM
but now I am trying to use the SWITCH/CASE script, but it is not working. I can't seem to find my mistake
Nor can we, as long we see no trace of your switch/case trial in your post. How can we see what is wrong as you have not posted what you consider to be wrong?

Lawds1
10-14-2008, 03:52 PM
OOOPs so sorry about that, I posted the wrong version. I posted my IF/Else script instead.

The fix above worked great though, thanks. I was missing a variable to process the users selection.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum