Click to See Complete Forum and Search --> : Need help with calorie script


unyun
12-27-2002, 09:29 PM
i'm having trouble with the popup box for gender

every time i hit the calculate button the popup selection reverts to "male" and the function will not return the "female"
numbers. this doesn't happen for my other popup "muscle" and yet they look the same.

any idea what i've done wrong?
here's the script i wrote: (the entire html document is attached as a txt file)

var ht_feet, ht_inches, current_weight, desired_weight, height, age, muscle, calorie_intake;

function calc_calories() {
current_weight = Number(document.calories.current_weight.value);
desired_weight = Number(document.calories.desired_weight.value);
height = Number(document.calories.ht_feet.value) * 12 + Number(document.calories.ht_inches.value);
age= Number(document.calories.age.value);

if (window.document.calories.muscle.options[0].selected == true) // coefficient for muscularity
{
muscle = .93;
} else {
if (window.document.calories.muscle.options[1].selected == true) {
muscle = 1;
} else {
if (window.document.calories.muscle.options[2].selected == true) {
muscle = 1.07;
} else {
alert ("Please enter muscularity")
}
}
}

if (age<18) {
alert("You must be 18 or older to get calorie recommendations");
}

if (window.document.calories.gender.options[0].selected = true) { // if male
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1))));
} else { // if female
calorie_intake = muscle * (1.25 * (665 + (4.36 * desired_weight) + (4.32 * height) - (4.7 * (age+1))));
}

document.calories.rec_calories.value = Math.round(calorie_intake);

}

thanks much!

mark

Zach Elfers
12-27-2002, 09:53 PM
Originally posted by unyun
i'm having trouble with the popup box for gender

every time i hit the calculate button the popup selection reverts to "male" and the function will not return the "female"
numbers. this doesn't happen for my other popup "muscle" and yet they look the same.

any idea what i've done wrong?
here's the script i wrote: (the entire html document is attached as a txt file)

var ht_feet, ht_inches, current_weight, desired_weight, height, age, muscle, calorie_intake;

function calc_calories() {
current_weight = Number(document.calories.current_weight.value);
desired_weight = Number(document.calories.desired_weight.value);
height = Number(document.calories.ht_feet.value) * 12 + Number(document.calories.ht_inches.value);
age= Number(document.calories.age.value);

if (window.document.calories.muscle.options[0].selected == true) // coefficient for muscularity
{
muscle = .93;
} else {
if (window.document.calories.muscle.options[1].selected == true) {
muscle = 1;
} else {
if (window.document.calories.muscle.options[2].selected == true) {
muscle = 1.07;
} else {
alert ("Please enter muscularity")
}
}
}

if (age<18) {
alert("You must be 18 or older to get calorie recommendations");
}

if (window.document.calories.gender.options[0].selected = true) { // if male
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1))));
} else { // if female
calorie_intake = muscle * (1.25 * (665 + (4.36 * desired_weight) + (4.32 * height) - (4.7 * (age+1))));
}

document.calories.rec_calories.value = Math.round(calorie_intake);

}

thanks much!

mark

This line:
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1))));

should be:
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1)));


The problem was you had 1 too many )'s after (age+1)

jeffmott
12-27-2002, 11:17 PM
every time i hit the calculate button the popup selection reverts to "male" and the function will not return the "female"
numbers

I don't see any form control named "gender" in the page you uploaded.

This line:
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1))));

should be:
calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1)));


The problem was you had 1 too many )'s after (age+1)

The originally poster's code already has matching number of parentheses. You managed to pick out a line of code that was just fine and "correct" it to make it wrong.

unyun
12-28-2002, 12:11 AM
dave -
sorry about the breach of etiquette...
my first post (obviously) on this forun
thanks for your patience and help
mark

unyun
12-28-2002, 12:20 AM
jeffmott -

isn't this the code for the 'gender' form control?

<select name="gender" size="1" tabindex="5">
<option value="0">male</option>
<option selected value="1">female</option>
</select>

?


i think the parentheses were right...
-mark

jeffmott
12-28-2002, 12:35 AM
unyun
isn't this the code for the 'gender' form control?

I'm sure it would be, but that code doesn't appear anywhere in the attachment to your first post. Perhaps you uploaded the wrong page?

unyun
i think the parentheses were right...

Yes, they're fine. Don't worry about it. Some people on this forum try to talk big but rarely (if ever) offer correct and/or helpful information.

unyun
12-28-2002, 04:57 AM
jeffmott -
YUP! yur right! i DID upload the wrong page... here's the right one, sorry! good pickup!
mark

unyun
12-28-2002, 05:11 AM
didn't attach for some reason... must be too large (?)

here's the link:

http://www.drliponis.com/calories.html


thanks again for all your help
mark

jeffmott
12-28-2002, 11:36 AM
if (window.document.calories.gender.options[0].selected = true) { // if male
&nbsp; &nbsp; calorie_intake = muscle * (1.25 * (66 + (6.23 * desired_weight) + (12.7 * height) - (6.8 * (age +1))));
} else { // if female
&nbsp; &nbsp; calorie_intake = muscle * (1.25 * (665 + (4.36 * desired_weight) + (4.32 * height) - (4.7 * (age+1))));
}

You have used one equal sign instead of two. One equal sign represents the assignment operation while two is the logical comparison. So this will set the first option of gender to true rather than checking to see if it is true.

unyun
12-29-2002, 05:35 PM
Of course!! Now I see the error of my ways!! I have seen the light! Many thanks for your help and persistence... this is a tremendous help thank you thank you thank you

...from a newbie climbing up the javascript learning curve...
mark