Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please Help to debugg my script (Switch/case)

    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:
    Code:
    <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:
    Code:
    <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>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    Code:
    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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Lawds1 View Post
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •