PDA

View Full Version : JavaScript for performing an average calculation

janihenn
06-01-2003, 05:59 AM
I need to write a script to perform an average calculation. I'm asking for a person's gender, age, weight, and height and have to compare it to a table and give the average. This is for my online college Web class. I have had no success in finding out how to do this. Any help would be greatly appreciated.

Thank you!

Janice

ACJavascript
06-01-2003, 05:19 PM
What kind of averege.

Like if the average hieght and weight of a 18 year old is: bla bla ?

if so then your really just trying to find the average between the weight and hieght.. right?

janihenn
06-01-2003, 06:57 PM
with nesting IF's. This is all new to me so.....If you have time to look it over, that would be great. If not, I'll send it to my instructor.

If I pull out the coding at the ELSE statement pertaining to female, it works fine, although if I type in female at the gender
prompt, I jump to height for males. I've been trying to figure this out for 5 hours now and I'm stuck. It doesn't help that our textbook doesn't go into details.
Here the coding:

<html>
<title>JavaScript Lab 11</title>
<body>
<SCRIPT LANGUAGE = "JavaScript">

document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");

var gender

if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")

else (( (gender == "female") || (gender == "Female") || (gender == "FEMALE") )
{
if( (weight>130) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
if( (height>64) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
</SCRIPT>
</BODY>
</HTML>

shlagish
06-01-2003, 07:09 PM
And that script doesn't work?

Mr J
06-01-2003, 07:19 PM
although if I type in female at the gender

That is because you have male written in the female section.

Try this

<html>
<title>JavaScript Lab 11</title>
<body>
<SCRIPT LANGUAGE = "JavaScript">

document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");

var gender

if ( (gender == "male") || (gender == "Male") || (gender == "MALE") ){

if( (weight>150) ){
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else{
alert("Your weight is below average for males between the ages of 20 and 50.")
}

if( (height>70) ){
alert("Your height is above average for males between the ages of 20 and 50.")
}
else{
alert("Your height is below average for males between the ages of 20 and 50.")
}
}

if ( (gender == "female") || (gender == "Female") || (gender == "FEMALE") ){

if( (weight>130) ){
alert("Your weight is above average for female between the ages of 20 and 50.")
}
else{
alert("Your weight is below average for female between the ages of 20 and 50.")
}

if( (height>64) ){
alert("Your height is above average for female between the ages of 20 and 50.")
}
else{
alert("Your height is below average for female between the ages of 20 and 50.")
}
}

</SCRIPT>
</BODY>
</HTML>

06-01-2003, 07:22 PM
try this

<title>JavaScript Lab 11</title>
<body>
<SCRIPT LANGUAGE = "JavaScript">

document.write("<H1><B>Get your Height and Weight Average</H1></B><BR>");
document.write("Your average calculation will be based on men and women aged 20 to 50.");

if (gender.toLowerCase()=="male")
{
Male();
}

if(gender.toLowerCase()== "female")
{
Female();
}

function Male()
{
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")

if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")
}

function Female()
{
if( (weight>130) )
{
alert("Your weight is above average for females between the ages of 20 and 50.")
}
else
alert("Your weight is below average for females between the ages of 20 and 50.")

if( (height>64) )
{
alert("Your height is above average for females between the ages of 20 and 50.")
}
else
alert("Your height is below average for females between the ages of 20 and 50.")
}
</SCRIPT>
</BODY>
</HTML>

janihenn
06-01-2003, 07:29 PM
It works beautifully now! I'm just learning so this forum is great! Now, I'm going to compare the one that works with mine to see the difference so I'll know where I made my errors!

Thank you all for such quick replies! I'll be able to finish my homework assignment today!

06-01-2003, 07:39 PM
just a suggestion if u want to make scripts in the future indent ur code it makes it a lot easier to find the fault u made :)

eaxmple ur code:
if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}
if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")

now with indents :

if ( (gender == "male") || (gender == "Male") || (gender == "MALE") )
{
if( (weight>150) )
{
alert("Your weight is above average for males between the ages of 20 and 50.")
}
else
alert("Your weight is below average for males between the ages of 20 and 50.")
}

if( (height>70) )
{
alert("Your height is above average for males between the ages of 20 and 50.")
}
else
alert("Your height is below average for males between the ages of 20 and 50.")

as u see if u had done this u could have seen that the if sentence where u check if the first promt i male ends to early

janihenn
06-01-2003, 07:52 PM
That's a great idea! Thanks for your help!!!!!

janihenn
06-01-2003, 09:55 PM
How could I put in code that if someone types anything other than what the prompt requests, they will get a prompt telling them what information needs to be typed?

Thanks for any suggestions!

Janice

Saj
06-01-2003, 10:25 PM
Try this:

if (isNAN(weight) || weight == "") {
}

if (gender != "male" || gender != "Male || gender != "MALE" || gender != "female" || gender != "Female || gender != "FEMALE")
alert("Please enter either male, Male, or MALE for Male, or female, Female or FEMALE for female");
}

And something to that effect for the rest.

janihenn
06-01-2003, 10:30 PM
Thank you! I'll give it a try!