...

View Full Version : BMI Calculator - Novice cannot get it working!



stuppy
01-13-2012, 01:36 AM
Hello

I have been asked by a friend to put together a BMI calculator. I know there are javascript ones already out there that I could use the code for, but

a) it needs to do a specific thing
b) I am interested in learning a bit of jscript - hence why I just spent about 4hrs trying to get it working :)

You can see what I have been working on here

http://www.kubedp.co.uk/bmi2.html

Basically - what it needs to do is the following :-
- User can either enter Feet/Inches or centimeters for height
- User can either enter Stones/Pounds or Kilos for weight.
- If they enter Feeet/Inches and centimeters - it throws an error (similar thing for weight).

It then outputs the BMI to txtBMIResults.

I managed to get the validation working on not having both Stones/Pounds and Kilos filled in etc, but when it comes to the actual calculation - I am stumped.

If someone could look at this for me and hopefully code it and comment it so I can understand what has been done to get it working so I might learn, that would be fantastic.

http://www.bmi-calculator.net/bmi-formula.php

Is the formulas for calculating BMI.

Not sure if you can tell from the code - but I was trying to get it so that the calculation was always done on Kilos and Meters, so I was taking the Stones and Pounds and feet and inches and trying to convert it to kilos and metres and then run the calculation.

Hope I have been clear.

Many thanks for any help anyone can offer

xelawho
01-13-2012, 01:53 AM
you need to look at your code using an error console. I like the firebug plug in for firefox, others prefer the built-in one in Chrome.

Anyway. The first error that pops is:

invalid assignment left-hand side
document.form.txtWeight_Kilos.value = "" &&

in javascript = assigns a value to a variable and == compares two variables. You can have a look here (http://www.w3schools.com/js/js_comparisons.asp) and here for more info

stuppy
01-13-2012, 02:25 PM
Hurrah!

Thank you for that. I have installed Firebug now - sure it will come in handy!.

Using your tip about the == I have managed to get it working.

http://www.kubedp.co.uk/BMICalculator.html

I know to most avid scripters this will be a nothing script - but I feel like I have accomplished something by getting this working.

Thank you again for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum