javascript calculator

I have some formulas that I want processed once you enter your info into the input fields and press the calculate button. I have the formulas working when i hardcode the the info directly into the variables but have had a hard time following what I should do to get everything to work from the input fields.

Could someone show me the modifications that need to be made to get this to work?
<!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>
 
 
 
<script type="text/javascript">
var height = 73;
var weight = 100;
var waist = 34;

var idealwaist = height * 0.382;
var idealshoulders = idealwaist * 1.618;
var hips = idealwaist * 1.42;

var waisttoheightratio = height / waist;

var waistdifference = idealwaist - waist;

var calorieintake = weight * (waistdifference - 12);
var maxloss = weight * [waistdifference * (0.005 * (waisttoheightratio - 1))] + 0.3;
var water = weight * 0.025;
function idealwaistfemale() {
	document.write(idealwaist); }
function idealshouldersfemale() {document.write(idealshoulders)}
function idealhipsfemale() {document.write(hips)}
function calorieintakefemale() { if ( 800 < calorieintake)
  {
  document.write(calorieintake);
  }
else
  {
  document.write(800);
  }}

function maxlossfemale() {document.write(maxloss)}

function waterfemale() {document.write(water)}

function fasting() { if ( idealwaist + 6 < waist)
  {
  document.write("72 hours");
  }
else if (idealwaist + 4 < waist )
  {
  document.write("48 hours");
  }
  else if (idealwaist + 1.5 < waist )
  {
  document.write("24 hours");
  }
  
  else
  document.write("24 hours, only as needed");
  }

</script>
 </head><body>

  <FORM NAME="Calculator" METHOD="post">
<P>Height: <INPUT TYPE=TEXT NAME="height" SIZE=10></P>
<P>Weight: <INPUT TYPE=TEXT NAME="weight" SIZE=10></P>
<P>Waist: <INPUT TYPE=TEXT NAME="waist" SIZE=10></P>
<P><INPUT TYPE="button" VALUE="Calculate" name="AddButton"></P>


</FORM>
          
 
                  
1) idealwaist female:<script type="text/javascript">idealwaistfemale();</script><br /><br />
2) idealshoulders female:<script type="text/javascript">idealshouldersfemale();</script><br /><br />
3) idealhips female:<script type="text/javascript">idealhipsfemale();</script><br /><br />
4) Suggested Daily Calorie Intake:<script type="text/javascript">calorieintakefemale();</script><br /><br />
5) Maximal weekly fat loss:<script type="text/javascript">maxlossfemale();</script><br /><br />
6) Possible daily water weight fluctuations:<script type="text/javascript">waterfemale();</script><br /><br />
7) Longest suggested fast:<script type="text/javascript">fasting();</script><br /><br />
</body></html>

Open in new window

turtleman2009Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nap0leonCommented:
to get a value from a form field, give all of your inputs a unique ID and use:
document.getElementById('inputhere').value

to turn that input into a number, you need to put a "parseFloat" around it:
parseFloat(document.getElementById('inputhere').value);

to make it read the values and recalculate, you need to put your var declarations into a function so that they can be recalculated.

To update the values displayed, you have to use an element of some sort, a "span" works as well as anything... and use:
docuement.getElementById('spanidhere').innerHTML = somevalue;

Here you go:
<!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>
 
<script type="text/javascript">

function reCalc(){
	//var height = 73;
	//var weight = 100;
	//var waist = 34;

	var height = parseFloat(document.getElementById('height').value);
	var weight = parseFloat(document.getElementById('weight').value);
	var waist = parseFloat(document.getElementById('waist').value);

	var idealwaist = height * 0.382;
	var idealshoulders = idealwaist * 1.618;
	var hips = idealwaist * 1.42;

	var waisttoheightratio = height / waist;

	var waistdifference = idealwaist - waist;

	var calorieintake = weight * (waistdifference - 12);
	var maxloss = weight * [waistdifference * (0.005 * (waisttoheightratio - 1))] + 0.3;
	var water = weight * 0.025;

	document.getElementById('idealwaist').innerHTML = idealwaist;
	document.getElementById('idealshoulders').innerHTML = idealshoulders;
	document.getElementById('idealhips').innerHTML = hips;

	if ( 800 < calorieintake) {
		document.getElementById('calorieintakefemale').innerHTML = calorieintake;
	} else {
		document.getElementById('calorieintakefemale').innerHTML = '800';
	}

	document.getElementById('maxlossfemale').innerHTML = maxloss;
	document.getElementById('waterfemale').innerHTML = water;

	if ( idealwaist + 6 < waist) {
		document.getElementById('fasting').innerHTML = '72 hours';
	} else if (idealwaist + 4 < waist ) {
		document.getElementById('fasting').innerHTML = '48 hours';
	} else if (idealwaist + 1.5 < waist ) {
		document.getElementById('fasting').innerHTML = '24 hours';
	} else {
		document.getElementById('fasting').innerHTML = '24 hours, only as needed';
	}

}
</script>
 </head><body>

  <FORM NAME="Calculator" METHOD="post">
<P>Height: <INPUT TYPE=TEXT NAME="height" id="height" SIZE=10></P>
<P>Weight: <INPUT TYPE=TEXT NAME="weight" id= "weight" SIZE=10></P>
<P>Waist: <INPUT TYPE=TEXT NAME="waist" id="waist" SIZE=10></P>
<P><INPUT TYPE="button" VALUE="Calculate" name="AddButton" onclick="javascript:reCalc();"></P>


</FORM>
          
 
                  
1) idealwaist female:<span id="idealwaist"></span><br /><br />
2) idealshoulders female:<span id="idealshoulders"></span><br /><br />
3) idealhips female:<span id="idealhips"></span><br /><br />
4) Suggested Daily Calorie Intake:<span id="calorieintakefemale"></span><br /><br />
5) Maximal weekly fat loss:<span id="maxlossfemale"></span><br /><br />
6) Possible daily water weight fluctuations:<span id="waterfemale"></span><br /><br />
7) Longest suggested fast:<span id="fasting"></span><br /><br />
</body></html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
turtleman2009Author Commented:
Perfect, thank you for the answer and the explanation!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.