www.webdeveloper.com
Results 1 to 4 of 4

Thread: Urgent debugging help needed

  1. #1
    Join Date
    Jun 2014
    Posts
    9

    Question Urgent debugging help needed

    I believe it may be with the if closing but I can't get my head around it and the homework piece is due in 12hours!!

    PHP Code:
    <!DOCTYPE html>
        <
    html>
        <
    head>
        <
    titleBMI calculator </title>
        </
    head>
        <
    body>
        <
    script language="JavaScript">
            var 
    ageprompt("Are you 18 or older?");
            if (
    age === 18){
                    var 
    weightprompt("Enter your weight in kilo's");
                    var 
    lengthprompt("Enter your height in centimeters"); }

                    while (
    weight 500 || weight || length 300 || length 0.4){   
                        if (
    length 300 || length 0.4){
                                    
    length prompt("Enter your height in kilo's");
                        } else if (
    weight 500 || weight 0){
                                    
    weight prompt("Enter your weight in kilo's");
                            }
                        }
                    }
                  var 
    bmi Math.round((weight 100) / (length length));


                    if (
    bmi >40) { 
                            
    confirm("Your BMI is" bmi ".You suffer from extreme obesity.");
                        else if (
    bmi 30 && bmi <=40)
                            
    confirm("Your BMI is" bmi ". You suffer from obesity.");
                        else if (
    bmi 25 && bmi <=30)
                            
    confirm("Your BMI is" bmi ". You are overweight.");
                        else if (
    bmi 18 && bmi <=25)
                            
    confirm("Your BMI is" bmi ". You have a normal BMI.");
                        else if (
    bmi 18)
                            
    confirm("Your BMI is" bmi ". You are underweight.");
                    }
                    }
            else {
            
    confirm("You must be 18 or older to be able to accurately calculate your BMI.")
            }
        
    </script>
        </body>
        </html> 
    Much love if you can help

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Have you checked the error console?
    There is a miss-match of the number of { and } pairs. (Probably in your nested IF...ELSE statements)

    Note also that the checks occur ONLY if the user is 18yo.
    And the question could be answered as 'yes' or 'no' or 'YES' or 'NO' or 'Yes' or 'nO', etc.
    Last edited by JMRKER; 06-12-2014 at 01:17 PM.

  3. #3
    Join Date
    May 2014
    Posts
    897
    some advice -- if you have already checked a value, don't check it again! See all those else? You already checked for >40, for it to else it is already <=40, you don't have to check that again!

    Likewise if you are declaring multiple VAR in a row, you can say var just once and make it a comma delimited list.

    Of course I'd probably use inline evaluation to simplify it and reduce the total code, and probably predeclare weight and height as false so another inline eval/expression can be leveraged to show an error message if they screw up entry. Likewise if you use do{}while instead of while you can skip coding the same question twice. Also, not sure it makes sense to be doing a 'confirm' instead of an 'alert' for the result.

    Also your formula is all wrong, you need to divide height by 100 to convert to meters BEFORE you find it's square, not divide what it's dividing by 100. Your version is guaranteed to return 0 thanks to that Math.round.

    Code:
    	var
    		age = prompt('Please enter your age'),
    		weight = false,
    		length = false; 
    	
    	if (age >= 18) { 
    	
    		do {
    			weight = prompt('Enter your weight in kilos' + (
    				weight === false ? '' : ' - must be between 1 and 500 kilos'
    			));
    		} while ((weight > 500) || (weight < 1));
    		
    		do {
    			length = prompt('Enter your length in centimeters' + (
    				length === false ? '' : ' - must be between 0.4 and 300 centimeters'
    			));
    		} while ((length < 0.4) || (length > 300));
    		
    		length /= 100;
    		
    		var bmi = Math.round(weight / (length * length)); 
    			
    		alert('Your BMI is ' + bmi + '. ' + (
    			bmi > 40 ? 'You suffer from extreme obesity.' : (
    			bmi > 30 ? 'You suffer from obesity.' : (
    			bmi > 25 ? 'You are overweight.' : (
    			bmi > 18 ? 'You have a normal BMI.' : (
    			'You are underweight'
    		))))));
    
    	} else alert('You must be 18 or older to be able to accurately calculate your BMI.');
    NOT that I usually go around doing people's homework for them... Sorry, it just smells slightly of something a teacher would come up with.
    Java is to JavaScript as Ham is to Hamburger.

  4. #4
    Join Date
    Jun 2014
    Posts
    9
    Holy moly thanks so much both of you guys!
    That saved me a weeks of detentions!
    (1=1hour/s) so 5 hours!!
    I was so confused I read through the error log for the first time which helped me discover many problems!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles