Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: JavaScript for performing an average calculation

1. ## JavaScript for performing an average calculation

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

• 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?

• ## I'm doing it differently now but I'm still stuck

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>

• And that script doesn't work?

• 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>

• try this

Code:
```<head>
<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>```

• ## Thank you all for the help!!!

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!

• 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 :
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.")```

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

• That's a great idea! Thanks for your help!!!!!

• ## error checking

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

• Try this:

Code:
``` var weight = prompt("Please tell me your weight","number in pounds");
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.

• Thank you! I'll give it a try!

• if u lower case gender as I did u can minimise ur if sentences, which in theory should make u script faster as it checks up on less

• Cool! Thanks! I'll give that a try also!

•

#### Posting Permissions

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